JSON and Jsonpath

Source: Internet
Author: User
Tags xpath

JSON (JavaScript Object Notation) is a lightweight data interchange format that is widely used in current data collation and collection because of its good readability and ease of machine parsing and generation.

JSON and XML are comparable.

Python 2.X comes with a JSON module that can be used directly from the import JSON.

Official Document: Http://docs.python.org/library/json.html

JSON online parsing website: http://www.json.cn

Json
JSON is simply the object and array of JavaScript, so these two structures are objects and arrays of two structures, which can represent a variety of complex structures.

Object: Object in JS is represented as {} in the content, data structure is {key1:value1, key2:value2, ...} The structure of the key-value pairs, in object-oriented language, key is the property of the object, value is the corresponding property value, so it is easy to understand that the value method is the object. Key Gets the property value, the type of the property value can be a number, a string, an array, an object.
Arrays: Arrays in JS are [] enclosed content, data structures for [' Python ', ' JavaScript ', ' C + + ', ...], in the same way as all languages, using index get, the type of field value can be a number, string, array, object.
JSON module

The JSON module provides four functions: dumps, dump, loads, load, for converting between string and Python data types.

1.json.dumps ()

To convert the Python type to a JSON string, return a str object, from Python to JSON, type conversions against the following:

#-*-Conding:utf-8-*-
Import JSON

LISTSTR = [1, 2, 3, 4]
Tuplestr = (1, 2, 3, 4)
Dictstr = {"City": "Beijing", "name": "Ant"}

Print (Json.dumps (LISTSTR))
# [1, 2, 3, 4]

Print (Type (json.dumps (LISTSTR)))
# <class ' str ' >

Print (Json.dumps (TUPLESTR))
# [1, 2, 3, 4]

Print (Type (json.dumps (TUPLESTR)))
# <class ' str ' >

# Note: The ASCII encoding used by default when Json.dumps () is serialized
# Add parameter ensure_ascii=false disable ASCII encoding and press UTF-8 encoding
Print (Json.dumps (dictstr, ensure_ascii = False))
# {"City": "Beijing", "name": "Ant"}

Print (Type (Json.dumps (dictstr, ensure_ascii = False)))
# <class ' str ' >
2.json.dump ()

Serializing a python built-in type to a JSON object after writing to a file

#-*-Conding:utf-8-*-
Import JSON

Liststr = [{"City": "Beijing"}, {"name": "Ant"}]
Json.dump (LISTSTR, open ("Liststr.json", "w", encoding = "Utf-8"), Ensure_ascii = False)

Dictstr = {"City": "Beijing", "name": "Ant"}
Json.dump (DICTSTR, open ("Dictstr.json", "w", encoding = "Utf-8"), Ensure_ascii = False)
3.json.loads ()

The JSON format string is decoded into a Python object, and the type conversions from JSON to Python are compared as follows:

#-*-Conding:utf-8-*-
Import JSON

Strlist = ' [1, 2, 3, 4] '

Strdict = ' {' City ': ' Beijing ', ' name ': ' Ant '} '

Print (Json.loads (strlist))
# [1, 2, 3, 4]

# JSON data is automatically stored by utf-8
Print (Json.loads (strdict))
# {' City ': ' Beijing ', ' name ': ' Ant '}
4.json.load ()

Read a JSON-like string in a file and convert it to a Python type

#-*-Conding:utf-8-*-
Import JSON

Strlist = json.load (Open ("Liststr.json", "r", encoding = "Utf-8"))
Print (strlist)
# [{' City ': ' Beijing '}, {' name ': ' Ant '}]

Strdict = json.load (Open ("Dictstr.json", "r", encoding = "Utf-8"))
Print (strdict)
# {' City ': ' Beijing ', ' name ': ' Ant '}
JsonPath
Jsonpath is an information extraction class library that extracts the specified information from a JSON document and provides multiple language implementations, including: JavaScript, Python, PHP, and Java.

Jsonpath, for JSON, is equivalent to XPath for XML.

: Https://pypi.python.org/pypi/jsonpath
Installation method: Download after extracting and then execute Python setup.py install
Official Document: Http://goessner.net/articles/JsonPath
Jsonpath vs. XPath syntax:

The Jsonpath structure is clear, readability is high, the complexity is low, very easy to match, the following table corresponds to the use of XPath.

Example:

To pull the net City JSON file: Http://www.lagou.com/lbs/getAllCitySearchLabels.json For example, get all the city names.

#-*-Conding:utf-8-*-
Import urllib.request
Import JSON
Import Jsonpath

# pull Hook net city json file
url = ' Http://www.lagou.com/lbs/getAllCitySearchLabels.json '
# user-agent Headers
Header = {' user-agent ': ' mozilla/5.0 ( Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.71 safari/537.36 '}

# URL, together with headers, constructs request requests , this request will be accompanied by the Chrome browser's user-agent
request = urllib.request.Request (URL, headers = header)

# Send this request to the server
Response = Urllib.request.urlopen (Request)

# Get Page content: bytes
html = response.read ()

# transcoding: bytes to str
html = Html.decode ("Utf-8")

# Converts a JSON-formatted string into a Python object
obj = json.loads (html)

# starting at the root node, matching the name node
City_list = Jsonpath.jsonpath (obj, ' $ '). Name ')

# Print Get name node
Print (city_list)
# print its type
print (Type (city_list))

# Write to Local disk file
with Open ("City.json", "w", encoding = "utf-8") as f:
content = Json.dumps (city_list, ensure_ascii = False)
F.write (con Tent)

JSON and Jsonpath

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.