python – TypeError:JSON对象必须是str,而不是’dict’

前端之家收集整理的这篇文章主要介绍了python – TypeError:JSON对象必须是str,而不是’dict’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对不起伙计们,我找不到令人满意的答案来打印部分json回复.有人可以帮助我吗:
import json
import requests
import pprint 

response = requests.get('<api endpoing>')
json_data = response.json()
print(json.dumps(json_data,indent=4,sort_keys=True))

Json的回应是

{
    "Value1": "SomeValue","data": {
        "subval1": false,"subval2": "0a4","subval3": "","subval4": "Click h!","subval5": "1002","subval6": "932","subval7": "i2","subval8": 250,"subval9": 0,"subval10": 1,"subval11": 3,"subval12": 1,"subval13": "<!>","subval14": "","subval15": "Click !!","subval16": "","subval17": 300
    },"error": true,"message": "Success","status": 200
}

现在,我想遍历并打印“数据”:值.我会做以下事情

data = json.loads(json_data)
data_set = (data['data'])
print(data_set)

但我得到的错误:TypeError:JSON对象必须是str,而不是’dict’

解决方法

你不需要json.loads(json_data),因为它已经是一个python dict,你只需要直接输出这个dict.从dict输出json字符串是json.dumps()的工作:
json.dumps(json_data["data"])
原文链接:https://www.f2er.com/python/186555.html

猜你在找的Python相关文章