[Python]json Object Conversion Error expected string or buffer Python

Source: Internet
Author: User

"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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.