Kivy URLRequest and Python Unicode and JSON

Source: Internet
Author: User

URLRequest

The content is also decoded (decoding) if the ContentType is Application/json and the result automatically (automatically) passed T Hrough json.loads.

Examples of official documents:

defGot_weather (req, results): forKey, Valueinchresults['Weather'][0].items ():Print(Key,': ', value)
Req=URLRequest ('HTTP://API.OPENWEATHERMAP.ORG/DATA/2.5/WEATHER?Q=PARIS,FR', got_weather)

Where Got_weather is the on_success handler function (Hook?) for which the request succeeds, and the second parameter in Got_weather is the requested response, which is results

It is important to note that results is data that has been processed by Json.loads.

That is, "raw data" is processed and becomes the data of Python. JSON corresponds to Python's data transformation as follows:

Json Python
Object Dict
Array List, tuple
String STR, Unicode
Number int, long, float
True True
False False
Null None

Unicode

This part has been summed up.

Json

Chestnuts:

<type'Dict'>{u'ERRORCODE': U' the', u'RESULT': U'Not matched'}<type'Dict'>{u'ERRORCODE': U'0', u'RESULT': {u'Nickname': U'Eric', u'name': U"', u'AccountID': U'KPRD'}}

Another chestnut:

('===========', <type'Unicode','==========='){"ERRORCODE":"ME01023","RESULT":"error!"}('===========', <type'Unicode','==========='){"ERRORCODE":"0","RESULT":{" Number":"794","Uniquecode":"841"}}

(current understanding) The first case is probably that the data passed in before Json.loads is already a JSON object, and after the json.loads of Ptyhon, the type is the Dict type, where the key value pairs the key and the value are ' The string ' type is converted into the Unicode representation of Python. such as U ' ERRORCODE '

In the second case, only the JSON string is converted to Unicode after it has been json.loads.

Provide examples of JSON operations in Python based on netizens:

#convert Python dict type to standard JSON stringD={}d['a'] =1d['b']=2d[3]='C'd[4]=['k','K1'] K=Jsonencoder (). Encode (d)Print(Type (k))Print(k)

Operation Result:

<type'Str'>{"a": 1,"3":"C","b": 2,"4": ["k","K1"]}

# convert JSON string to Python dict type json_str='{"A": 1, "B": 2, "3": "C", "4": ["K", "K1"]}' d=jsondecoder (). Decode (JSON_STR)print(type (d))print(d)

Operation Result:

<type'Dict'>{u'a': 1, U'3': U'C', u'b': 2, U'4': [u'k', u'K1']}

Field: Json.dumps is the inverse operation of Json.loads.

Write it here for the time being, and add it later.

Kivy URLRequest and Python Unicode and JSON

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.