In-depth analysis of the use of the JSON module in Python programming, pythonjson

Source: Internet
Author: User
Tags pprint

In-depth analysis of the use of the JSON module in Python programming, pythonjson

The basic data types supported by JSON encoding are None, bool, int, float, and str, and the lists, tuples, and dictionaries that contain the data types. For dictionaries, keys must be string type (any non-string key in the dictionary is converted to a string before encoding ). To comply with the JSON specification, you should only encode Python lists and dictionaries. Furthermore, in web applications, top-level objects are encoded as a dictionary, which is a standard practice.

The JSON encoding format is almost identical to the Python syntax except for some small differences. For example, True is mapped to true, False is mapped to false, and None is mapped to null. The following is an example of the encoded string effect:

>>> json.dumps(False)'false'>>> d = {'a': True,...   'b': 'Hello',...   'c': None}>>> json.dumps(d)'{"b": "Hello", "c": null, "a": true}'>>>

If you try to check the JSON decoded data, it is usually difficult to determine its structure through simple printing, especially when the nested structure of the data is deep or contains a large number of fields. To solve this problem, you can use the pprint () function of the pprint module to replace the normal print () function. It will be output in alphabetical order of keys in a more beautiful way. The following is an example of how to print and output the search results on Twitter:

>>> from urllib.request import urlopen>>> import json>>> u = urlopen('http://search.twitter.com/search.json?q=python&rpp=5')>>> resp = json.loads(u.read().decode('utf-8'))>>> from pprint import pprint>>> pprint(resp){'completed_in': 0.074,'max_id': 264043230692245504,'max_id_str': '264043230692245504','next_page': '?page=2&max_id=264043230692245504&q=python&rpp=5','page': 1,'query': 'python','refresh_url': '?since_id=264043230692245504&q=python','results': [{'created_at': 'Thu, 01 Nov 2012 16:36:26 +0000',      'from_user': ...      },      {'created_at': 'Thu, 01 Nov 2012 16:36:14 +0000',      'from_user': ...      },      {'created_at': 'Thu, 01 Nov 2012 16:36:13 +0000',      'from_user': ...      },      {'created_at': 'Thu, 01 Nov 2012 16:36:07 +0000',      'from_user': ...      }      {'created_at': 'Thu, 01 Nov 2012 16:36:04 +0000',      'from_user': ...      }],'results_per_page': 5,'since_id': 0,'since_id_str': '0'}>>>

Generally, JSON decoding creates dicts or lists based on the provided data. If you want to create other types of objects, you can pass object_pairs_hook or object_hook parameters to json. loads. For example, the following example shows how to decode JSON data and retain the order in an OrderedDict:

>>> s = '{"name": "ACME", "shares": 50, "price": 490.1}'>>> from collections import OrderedDict>>> data = json.loads(s, object_pairs_hook=OrderedDict)>>> dataOrderedDict([('name', 'ACME'), ('shares', 50), ('price', 490.1)])>>>

The following is an example of how to convert a JSON dictionary into a Python object:

>>> class JSONObject:...   def __init__(self, d):...     self.__dict__ = d...>>>>>> data = json.loads(s, object_hook=JSONObject)>>> data.name'ACME'>>> data.shares50>>> data.price490.1>>>

In the last example, the decoded JSON dictionary is passed to _ init _ () as a single parameter __(). Then, you can use it as you like. For example, you can use it directly as an instance dictionary.

Some options are useful when encoding JSON. If you want to obtain a nice formatted string and then output it, you can use the indent parameter of json. dumps. It will make the output function similar to the pprint () function. For example:

>>> print(json.dumps(data)){"price": 542.23, "name": "ACME", "shares": 100}>>> print(json.dumps(data, indent=4)){  "price": 542.23,  "name": "ACME",  "shares": 100}>>>

Object instances are not serialized in JSON format. For example:

>>> class Point:...   def __init__(self, x, y):...     self.x = x...     self.y = y...>>> p = Point(2, 3)>>> json.dumps(p)Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/usr/local/lib/python3.3/json/__init__.py", line 226, in dumps    return _default_encoder.encode(obj)  File "/usr/local/lib/python3.3/json/encoder.py", line 187, in encode    chunks = self.iterencode(o, _one_shot=True)  File "/usr/local/lib/python3.3/json/encoder.py", line 245, in iterencode    return _iterencode(o, 0)  File "/usr/local/lib/python3.3/json/encoder.py", line 169, in default    raise TypeError(repr(o) + " is not JSON serializable")TypeError: <__main__.Point object at 0x1006f2650> is not JSON serializable>>>

If you want to serialize an object instance, you can provide a function whose input is an instance and return a serializable dictionary. For example:

def serialize_instance(obj):  d = { '__classname__' : type(obj).__name__ }  d.update(vars(obj))  return d

If you want to obtain this instance in turn, you can do this:

# Dictionary mapping names to known classesclasses = {  'Point' : Point}def unserialize_object(d):  clsname = d.pop('__classname__', None)  if clsname:    cls = classes[clsname]    obj = cls.__new__(cls) # Make instance without calling __init__    for key, value in d.items():      setattr(obj, key, value)      return obj  else:    return d

The following is an example of how to use these functions:

>>> p = Point(2,3)>>> s = json.dumps(p, default=serialize_instance)>>> s'{"__classname__": "Point", "y": 3, "x": 2}'>>> a = json.loads(s, object_hook=unserialize_object)>>> a<__main__.Point object at 0x1017577d0>>>> a.x2>>> a.y3>>>

The json module also has many other options to control the parsing of lower-level numbers, special values such as NaN. For more details, see the official documentation.

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.