读取Json BugFix

遇到的错误如下所示:

遇到的错误如下所示:

File ~/miniconda3/lib/python3.9/json/decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
    344 """Decode a JSON document from ``s`` (a ``str`` beginning with
    345 a JSON document) and return a 2-tuple of the Python
    346 representation and the index in ``s`` where the document ended.
   (...)
    350 
    351 """
    352 try:
--> 353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
    355     raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting property name enclosed in double quotes: line 54022608 column 6 (char 987758592)

网上查询这个错误是JSon里面的双引号搞成了单引号,故作相应的替换;

将单引号替换为双引号,下面为相应的代码:

# 导入Python的JSON模块,该模块提供了解析JSON的函数  
import json  
  
# 定义要读取的JSON文件的路径  
json_path  = './example.json'  
  
# 使用with语句打开文件,这样可以确保文件在读取后会被正确关闭  
with open(json_path,'r') as f:  
  
    # 使用json模块的load函数从文件中读取JSON数据,并将其解析为Python对象  
    output = json.load(f)  
      
    # 使用json模块的dumps函数将Python对象转换为JSON格式的字符串  
    json_str = json.dumps(output)   
  
    # 使用字符串的replace函数将所有的单引号替换为双引号。这一步其实是不必要的,因为json模块在解析和生成JSON时默认使用双引号。  
    json_str = json_str.replace("'", "\"")  
  
    # 再次使用json模块,这次使用loads函数将修改后的JSON字符串重新解析为Python对象  
    json_obj = json.loads(json_str)   
  
    # 使用with语句打开一个新的文件用于写入,这个文件被命名为'replace.json'  
    with open('./replace.json','w+') as f:  
        # 使用json模块的dump函数将Python对象写入文件,参数indent设置为2表示生成的JSON数据将采用缩进格式,便于阅读  
        json.dump(json_obj, fp=f, indent=2)

水平有限,有问题随时交流~ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VB中读取JSON数据可以使用Json.NET库来进行操作。首先需要在项目中添加对Json.NET库的引用,然后可以通过以下步骤来读取JSON数据: 1. 导入相关命名空间: ```vb Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq ``` 2. 创建一个StreamReader来读取JSON文件或从字符串中读取JSON数据: ```vb Using reader As New StreamReader("path/to/json/file.json") Dim jsonContent As String = reader.ReadToEnd() End Using ``` 3. 使用`JsonConvert.DeserializeObject()`方法将JSON字符串转换为动态对象(`Object`)或指定的类型: ```vb Dim jsonData As Object = JsonConvert.DeserializeObject(jsonContent) ``` 或者,如果已知JSON的结构,可以定义一个类来表示JSON数据的结构,然后将其传递给`JsonConvert.DeserializeObject()`方法: ```vb Public Class Person Public Property Name As String Public Property Age As Integer End Class Dim personData As Person = JsonConvert.DeserializeObject(Of Person)(jsonContent) ``` 4. 如果JSON数据是一个数组,可以使用`JArray.Parse()`方法将其转换为`JArray`对象,并使用索引访问各个元素: ```vb Dim jsonArray As JArray = JArray.Parse(jsonContent) For Each item As JObject In jsonArray Dim name As String = item("name").ToString() Dim age As Integer = Integer.Parse(item("age").ToString()) ' 处理每个数组元素的数据 Next ``` 以上是使用VB读取JSON数据的基本步骤,根据具体的JSON结构和需要读取的字段,可以进行相应的操作和解析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值