[英]Expecting property name enclosed in double quotes - converting a string to a json object using Python
I am trying to convert a string to JSON using python using the following code: 我正在尝试使用以下代码使用python将字符串转换为JSON:
myStr = '[{u"total": "54", u"value": "54", u"label": u"16 Sep"}, {u"total": "58", u"value": "4", u"label": u"16 Sep"}, {u"total": "65", u"value": "7", u"label": u"16 Sep"}, {u"total": "65", u"value": "0", u"label": u"16 Sep"}]'
import json
json.loads(myStr)
I get the following error: 我收到以下错误:
ValueError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
This makes no sense as every property has double quotes, not single ones. 这没有任何意义,因为每个房产都有双引号,而不是单引号。 Any help? 有帮助吗?
Remove the unicode qualifier from the string. 从字符串中删除unicode限定符。 json.loads
assumes the property names are already in unicode. json.loads
假设属性名称已经是unicode。
import json
myStr = '[{"total": 54, "value": 54, "label": "u16 Sep"}, {"total": 58, "value": 4, "label": "u16 Sep"}, {"total": 65, "value": 7, "label":" u16 Sep"}, {"total": 65, "value": 0,"label": "u16 Sep"}]'
obj = json.loads(myStr)
print(repr(obj))
You try load incorrect JSON, you can check it here . 您尝试加载不正确的JSON,您可以在此处查看 。 I edit your json and it work. 我编辑你的json,它工作。
Putting together the puzzle pieces from this question and Getting SyntaxError: JSON Parse error: Expected '}' when trying to convert a string into JSON using javascript , you want to do: 汇总这个问题的拼图和获取语法错误:JSON解析错误:当尝试使用javascript将字符串转换为JSON时,预期'}' ,您想要:
myStr = [{u"total": "54", u"value": "54", u"label": u"16 Sep"}, {u"total": "58", u"value": "4", u"label": u"16 Sep"}, {u"total": "65", u"value": "7", u"label": u"16 Sep"}, {u"total": "65", u"value": "0", u"label": u"16 Sep"}]
import json
print(json.dumps(myStr))
Then copy paste the outputted string into javascript. 然后将输出的字符串复制粘贴到javascript中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.