题目描述
提取data中的x,并将其相加。
data ={"note":"This",
"count":[
{ "id" : "001","x" : "2","name" : "Chuck"} ,
{ "id" : "009","x" : "7", "name" : "Brent"}
]}
题目来源及自己的思路
用json提取
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import json
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
data ={"note":"This",
"count":[
{ "id" : "001","x" : "2","name" : "Chuck"} ,
{ "id" : "009","x" : "7", "name" : "Brent"}
]}
info = json.loads(data)
print('User count:', len(info))
for item in info:
print('Name', item['name'])
print('Id', item['id'])
print('Attribute', item['x'])
你期待的结果是什么?实际看到的错误信息又是什么?
1.我期待的结果是
x 2
x 7
2.但执行结果提示TypeError: the JSON object must be str, bytes or bytearray, not dict
3.我试了下如果将data转化为以下样式就可以了,但不知如何转化。另外,如果用data=data['count'],转化出来的数据不带”'''“,导致还是会报错。
data ='''[
{ "id" : "001", "x" : "2", "name" : "Chuck"} ,
{ "id" : "009", "x" : "7", "name" : "Brent"}
]'''