Knowledge about json File Parsing in Python,

Source: Internet
Author: User

Knowledge about json File Parsing in Python,

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation.

Parse a text file using python today in the following format:

[{"Key": "android. permission. ACCESS_CHECKIN_PROPERTIES "," Title ":" Access checked attributes "," Memo ":" read/write access to the attributes uploaded by the check service is allowed. Normal applications cannot use this permission. "," Level ": 0 },{" Key ":" android. permission. ACCESS_COARSE_LOCATION "," Title ":" approximate location "," Memo ":" Visit the approximate location source (such as the cellular network database) to determine the approximate location of the mobile phone (if possible ). Malicious applications can use this to determine your approximate location. "," Level ": 1 },{" Key ":" android. permission. ACCESS_COARSE_LOCATION "," Title ":" approximate location "," Memo ":" Visit the approximate location source (such as the cellular network database) to determine the approximate location of the mobile phone (if possible ). Malicious applications can use this to determine your approximate location. "," Level ": 1}]

Start to use open ('filepath '). the readlines () method is used to read, so that all the read content is stored in a list, but I want to retrieve the content in every {}, so I want to use split (',') the method is separated, and the content in each {} is also separated according. Later, I asked a netizen to read the data in json format. The following method is used:

#-*-Encoding: utf -- *-import jsonf = file (r 'C: \ Users \ Tim \ Desktop \ test. json ') jsonobj = json. load (f) # Use the serial number in the list to query print jsonobj [] ['memo'] f. close

The following error is reported after running:

ValueError: No JSON object cocould be decoded

Save the json file again in UTF8 without BOM mode and the operation is successful.

In addition, you can use the following methods to read json strings:

s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["", ""]}}')print sprint s.keys()print s["name"]print s["type"]["name"]print s["type"]["parameter"][]

The above content introduces you to the knowledge of parsing json files in Python, and I hope to help you!

Articles you may be interested in:
  • Python json parsing instance method
  • Example of parsing JSON in Python and customizing encoding at the same time
  • Basic Method for parsing JSON data using Python
  • In-depth analysis of the use of the JSON module in Python Programming

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.