"Problem"
Today in use JSON transformations in Python encounter a problem:
"Code"
Comments.json
{"Count": "2", "page": "1", "comments": [{"Content": "HelloWorld", "user": {"id": "0001", "name": "Xiaosi"}, "source": {" Link ":" http://mobile.youku.co "," name ":" IPhone "}},{" content ":" Welcome to China "," user ": {" id ":" 0002 "," name ":" SJF "} , "source": {"link": "http://mobile.youku.co", "name": "Android"}}]}
test.py
# coding=utf-8import Jsonfile = File ("d:\\ project \python\comments.json") data = json.loads (file)
"Analytical solution"
After debugging, it turns out that single quotation marks are used to denote the string "'" By default in Python, so when a string character value is used, Python converts the double quotation marks to single quotes.
Example:
s = {"Count": "2", "page": "1", "comments": [{"Content": "HelloWorld", "user": {"id": "0001", "name": "Xiaosi"}, "source": {" Link ":" http://mobile.youku.co "," name ":" IPhone "}},{" content ":" Welcome to China "," user ": {" id ":" 0002 "," name ":" SJF "} , "source": {"link": "http://mobile.youku.co", "name": "Android"}}]}print s
The JSON does not support single quotes. You can use the following method to convert
Json_string=json.dumps (s)
Str=json.loads (json_string)
[Python]json Object Conversion Error expected string or buffer Python