json和预期读取的不一样

文章描述了一个Python函数`json_to_text`,尝试将JSON文件的内容转换为文本格式,但遇到了类型错误,因为处理的是字符串而非字典。
摘要由CSDN通过智能技术生成
def json_to_text(json_file_path):
    # 读取JSON文件
    with open(json_file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    
    # 调试输出JSON数据结构
    print("JSON数据结构:", data)
    
    # 将JSON数据转换为文本格式
    text = ""
    for item in data:
        print("当前条目:", item)
        for key, value in item.items():
            text += f"{key}: {value}\n"
        text += "\n"  # 添加空行分隔不同的条目
    
    return text

/data/user/0/org.qpython.qpy/files/bin/qpy thon3.sh "/storage/emulated/0/qpython/json 处理2.py" && exit
python/json处理2.py" && exit             < Traceback (most recent call last):
  File "/storage/emulated/0/qpython/json处 理2.py", line 22, in <module>
    text_data = json_to_text(json_file_pat h)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
  File "/storage/emulated/0/qpython/json处 理2.py", line 12, in json_to_text
    for key, value in item.items():
                      ^^^^^^^^^^
AttributeError: 'str' object has no attrib ute 'items'
1|:/ $

在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值