Requests Requests method --- json module method - session method

1, Json Description: Json, full name JavaScript Object Notation, is a lightweight data interchange format, commonly used in the http request

2, can help (json), see comments corresponding to the content source

Encoding basic Python object hierarchies::
>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print json.dumps("\"foo\bar")
"\"foo\bar"
>>> print json.dumps(u'\u1234')
"\u1234"
>>> print json.dumps('\\')
"\\"
>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
{"a": 0, "b": 0, "c": 0}
>>> from StringIO import StringIO
>>> io = StringIO()
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'

3, Encode (Python -> json)
A, at first said why should encode, python inside bool value is True and False, json inside bool value is true and false, and is case sensitive, which is embarrassing, obviously it is bool value.
In python code written inside, spread in json, certainly not identify, so it is necessary to go through the python code encode data types json become recognizable.
B, give a simple example, the figure type dict through json.dumps (after) becomes str, True becomes true, False becomes fasle
C, the following correspondence table to climb out of the inside of the module from source json .python data classes, through to encode
json data type, corresponding to the following table
| | the Python | the jSON |
| + + ==== =================== + ===========
| | dict | Object |
| + ---------- + -------------- ----- + -----
| | List, tuple | Array |
| + ------------------- + -------------- - +
| | str, Unicode | String |
| + ------------------- + --------------- +
| | int, Long, float | Number The |
| + ------------------- + --------------- +
| | True | true |
| +-------------------+---------------+
| | False | false |
| +-------------------+---------------+
| | None | null |
| +-------------------+---------------+

 

4、 decode(json->python)

a, to Requests method - session method  , for example: { "success": true}, for example, we want to know is that the most success in this field returns True or False
b, if the content of bytes output, returns a string: { "success": true} , so that the results acquired later is not convenient
C, if json after decoding, is the return of a dictionary: {u'success': True}, so that the results acquired later, on the way to the value of the dictionary: result2 [ "Success"]
D, the same data into data python json recognizable, the corresponding relationship table below
| + --------------- + ------------------- +
| | JSON | Python |
| + + ===== =============== + ==============
| | Object | dict |
| + --------------- + ----------- + --------
| | Array | List |
| + ----------------- + --------------- - +
| | String | Unicode |
| + --------------- + ------------------- +
| | Number The (int) | int, Long |
| + --------------- + ------------------- +
| | number (real) | float |
| +---------------+-------------------+
| | true |
True |
| +---------------+-------------------+
| | false | False |
| +---------------+-------------------+
| | null |
None |
| +---------------+-------------------+

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11144853.html