Python processing JSON

Source: Internet
Author: User

First, what is JSON?

JSON is a lightweight format for data interchange

Second, Python's thinking of working with JSON

In fact, it is easy to understand the data into JSON format data and bar JSON format data parsing out

Third, the specific method of Python processing JSON

1, Json.dumps ()

The function can convert the simple data type (INT\FLOAT\STRING\TUPLE\LIST\DICT\UNICODE) to JSON format, the sample code is as follows:

Import Jsonsrc_data = {"Name": "Tacey", "age": +, "sex": "Male", "interst":("programing", "Reading")} #print repr (src_ Data) print Json.dumps (src_data)

The output is as follows:

{' Interst ':(' programing ', ' Reading '), ' age ': +, ' name ': ' Tacey ', ' sex ': ' Male '} {' interst ': ["programing", "Reading"], " Age ":", "" Name ":" Tacey "," Sex ": MAL"}

2, Json.loads ()

The function can convert the JSON data into a python simple data type, followed by the code above:

Json_data = Json.dumps (src_data) print json.loads (json_data) ["Name"]

Output Result:


Iv. JSON processing in flask

Flask can also be useful in Python's own JSON module, but also practical flask in the jsonify, sample code is as follows:

From flask import flask, Jsonifyapp = Flask (__name__) Json_data = [{' Name ': ' Tacey ', ' age ': $, ' sex ': ' Male ', ' interst ': ("programing", "Reading")} {"Name": "Amber", "Age": $, "sex": "female", "interst":("food", "Dog")}] @app. Route ('/jsontest ', methods=[' GET ') ) def Get_json (): Return jsonify ({' JSON ': Json_data}) if __name__ = = ' __main__ ': (debug=true)

"Note": Now is just a simple practical, some have not been involved, such as non-Python comes with standard data types, JSON complete division




Python processing 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: 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.