Python uses simplejson to parse JSON. simplejsonjson

Source: Internet
Author: User
Tags aliyun

Python uses simplejson to parse JSON. simplejsonjson

1. Introduction to the Json Module
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, 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.

2. Json format
2.1, object:

{Name: "Peggy", email: "peggy@gmail.com", homepage: "http://www.bkjia.com"} {property: value, property: value, property: Value}

2.2, array:
Is a set of ordered values. An array starts with "[", ends with "]", and values are separated.

[ {name:"Peggy",email:"peggy@gmail.com",homepage:"http://www.bkjia.com"}, {name:"Peggy",email:"peggy@gmail.com",homepage:"http://www.bkjia.com"}, {name:"Peggy",email:"peggy@gmail.com",homepage:"http://www.bkjia.com"} ] 
In addition, the value can be a string, number, true, false, null, or an object or array. These structures can be nested.

3. Json Import and Export
Write/dump indicates that the Json object is input to a python_object. If python_object is a file, it is dumped to the file. If it is an object, it is dumped to the memory. This is serialization.

3.1. Read the Json File

import simplejson as json f = file('table.json') source = f.read() target = json.JSONDecoder().decode(source) print target import simplejson as json jsonobject = json.load(file('table.json')) print jsonobject 

3.2. display the Json File
The original Json file is as follows:

[admin@r42h06016.xy2.aliyun.com]$python readJson.py [{'Query': 'desc zt1;', 'Message': '{"DescibeTableWithPartSpec": "false", "GetTableMetaString":"{\\"tableName\\":\\"zt1\\",\\"owner\\":\\"1365937150772213\\",\\"createTime\\":1346218114,\\"lastModifiedTime\\":0,\\"columns\\":[{\\"name\\":\\"a\\",\\"type\\":\\"string\\"},{\\"name\\":\\"b\\",\\"type\\":\\"string\\"}],\\"partitionKeys\\":[{\\"name\\":\\"pt\\",\\"type\\":\\"string\\"}]}"}', 'QueryID': '', 'Result': 'OK'}] 

Execution file:

import simplejson as json jsonobject = json.load(file('table.json')) print json.dumps(jsonobject,sort_keys=True,indent=4) 

Display:

[admin@r42h06016.xy2.aliyun.com]$python readJson.py [   {     "Message": "{\"DescibeTableWithPartSpec\": \"false\", \"GetTableMetaString\":\"{\\\"tableName\\\":\\\"zt1\\\",\\\"owner\\\":\\\"1365937150772213\\\",\\\"createTime\\\":1346218114,\\\"lastModifiedTime\\\":0,\\\"columns\\\":[{\\\"name\\\":\\\"a\\\",\\\"type\\\":\\\"string\\\"},{\\\"name\\\":\\\"b\\\",\\\"type\\\":\\\"string\\\"}],\\\"partitionKeys\\\":[{\\\"name\\\":\\\"pt\\\",\\\"type\\\":\\\"string\\\"}]}\"}",     "Query": "desc zt1;",     "QueryID": "",     "Result": "OK"   } ] 

3.3, json module example:

import json # Converting Python to JSON json_object = json.write( python_object ) # Converting JSON to Python python_object = json.read( json_object ) 

3.4, simplejson module example:

import simplejson # Converting Python to JSON json_object = simplejson.dumps( python_object ) # Converting JSON to Python python_object = simplejson.loads( json_object ) 

The json_object can also be a file name, such as file ("tmp/table. json ")

4. Json data parsing
Assume that the data. json file is as follows:

Copy codeThe Code is as follows:

{'Issuccess': True, 'errormsg ': '', 'Total': 1, 'data': [{'isonline': True, 'idc ': '\ xe6 \ x9d \ xad \ xe5 \ xb7 \ x9e \ xe5 \ xbe \ xb7 \ xe8 \ x83 \ x9c \ xe6 \ x9c \ xba \ xe6 \ x88 \ xbf ', 'assetsnum': 'b5007000007003', 'responsibilityperson': '\ xe5 \ xbc \ xa0 \ xe4 \ xb9 \ x8b \ xe8 \ xaf \ x9a', 'devicemodel ': 'poweredge 200', 'servicetag': '729hh2x', 'IP': '123. 16.20.163 ', 'hostname': 'hzshterm1 .alibaba.com', 'manageip': '2017. 31.58.223 ', 'Cabinet': 'h05 ', 'buckettime': '2017-06-29', 'usestate ': '\ xe4 \ xbd \ xbf \ xe7 \ x94 \ xa8 \ xe4 \ xb8 \ xad', 'memoryinfo': {'amount ': 4, 'SIZE': 8192 }, 'cpuinfo': {'corenum': 8, 'l2cachesize': 6144, 'amount': 2, 'model': 'intel (R) Xeon (R) CPU E5405 @ 2.00GHz ', 'masterfrequency': 1995}, 'cabinetpositionnum': '', 'outguaranteetime':'', 'logicsite ': '\ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 \ xe7 \ xab \ x99'}]}
First, import the file, create a Json object, and view the type. It is already of the dict type.
#test.py import simplejson as json ddata = json.loads(file("data.json")) print ddata print type(ddata)#<type 'dict'> 

Second, we use the key value corresponding to "data" as the key in the read dictionary.

>>> Ddata ['data'] // view the dictionary! >>> Type (ddata ['data']) <type 'LIST'>

Ddata ['data'] is a list, and the list must be queried by serial number.

>>> Ddata ['data'] [0] // Method for viewing the list! >>> Type (ddata ['data'] [0]) <type 'dict '>

The element 0 in the ddata ['data'] list is a dictionary ..
Okay. Let's check the idc key value.

>>> Ddata ['data'] [0] ['idc '] // you can view the dictionary. >>> Ddata ['data'] [0] ['idc '] // you can view the dictionary! '\ Xe6 \ x9d \ xad \ xe5 \ xb7 \ x9e \ xe5 \ xbe \ xb7 \ xe8 \ x83 \ x9c \ xe6 \ x9c \ xba \ xe6 \ x88 \ xbf'>> print ddata ['data'] [0] ['idc '] Hangzhou desheng data center

5. Some performance discussions

After a simple test, if JSON is used, that is, the built-in json processing library of python2.6 or above, the efficiency can be calculated as follows:
1 k Data, 36898 GHz CPU, single core per second dump: Times. It is about 5 times that of pyamf. However, the data volume is large, which is about 1.67 times (1101/656) of pyamf ).

start_time: 1370747463.77loop_num: 36898end_time:  1370747464.78

 
Let's take a look at simplejson. If the C extension is not installed:

Simplejson, without the C extension installed, I was surprised by the result:

start_time: 1370748132.87loop_num: 1361end_time:  1370748133.88

The efficiency is so low.
 
The following is the test code:

#! /Usr/bin/env python # coding = UTF-8 import time json test_data = {'weight': {'name': unicode ('lily', 'utf-8 '), 'put': unicode ('fresh, elegant, floral, utf-8'), 'grow _ time': 0.5, 'Fruit _ time': 0.5, 'Super _ time': 0.5, 'total _ time': 1, 'buy': {'gold': 2,}, 'Harvest _ fruit ': 1, 'Harvest _ super': 1, 'sale': 1, 'level _ need': 0, 'experience ': 2, 'exp _ fruit': 1, 'exp _ super': 1, 'used': True,}, '1': {'interval ': 0.3, 'bability ': {'98': {'chips ': (5, 25),}, '2': {'G ': (1000) ,},}, '2': {'unlock': {'chips ':, 'fc': 10,}, 'interval ': 12, 'bability ': {'70': {'chips': (120,250),}, '20': {'gem ': (),}, '10 ': {'gems ': () ,},},}, 'one': {'10, 5': {'id': 'm01', 'y ': 1, 'msg ': U' found a silver coin in the jar! ',}, '3, 7': {'id': 'm02', 'y': 10, 'msg': U' found ten silver coins! A huge amount of money! ',}, '15, 5': {'id': 'm03', 'y': 2, 'msg ': u'a mouse ran past ',}, '7, 4': {'id': 'm04', 'y': 4, 'msg ': u' found four Rusty silver coins ...... ',}, '2, 12': {'id': 'm05', 'y': 6, 'msg ': U' six shiny silver coins! ',} Start_time = time. time () print "start_time:", start_time j = 1 while True: j + = 1 a = json. dumps (test_data) data_length = len (a) end_time = time. time () if end_time-start_time> = 1: break print "loop_num:", j print "end_time:", end_time print data_length,

 
Summary: python's built-in json is acceptable in terms of performance. Simplejson, without C extension acceleration, is extremely inefficient.

Articles you may be interested in:
  • Make python json encode datetime type
  • Basic Method for parsing JSON data using Python
  • Brief Introduction to JSON usage in Python
  • Python reads json files and inserts data into mongodb
  • Python automatic formatting of json files
  • Examples of conversion between dictionaries and JSON in Python
  • Convert a dictionary to its json string in python
  • A simple example of using Python to operate json data
  • Python processes Chinese Characters in json data
  • Python json parsing instance method

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.