Python-json module "parsing json"

Source: Internet
Author: User
Tags aliyun
1,json Module Introduction

JSON (JavaScript Object notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy to machine parse and generate. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON uses a completely language-independent text format, but it also uses a family of C-language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language.
format of 2,json

2.1, Object:

{Name: "Peggy", Email: "Peggy@gmail.com", Homepage: "Http://www.peggy.com"}
{property: Value, Property: Value, property: Value}
2.2, array:

is a collection of sequential values. An array begins with "[", Ends with "]", and the values are separated by ",".

[
{name: "Peggy", Email: "Peggy@gmail.com", Homepage: "Http://www.peggy.com"}, {name: "Peggy", Email: " Peggy@gmail.com ", homepage:" Http://www.peggy.com "},
{name:" Peggy ", Email:" Peggy@gmail.com ", homepage:" http:// Www.peggy.com "}
]

Alternatively, the value can be a string, a number, true, false, NULL, or an object or an array. These structures can be nested.
Import Export of 3,json

The meaning of the write/dump here is to enter a JSON object into a python_object, dump it into a file if Python_object is a file, or dump it into memory if it is an object. 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, displaying the JSON file

To display the JSON format nicely, the original JSON file:

[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 '}]
Execute file:

Import Simplejson as json
jsonobject = json.load (file (' Table.json '))
print json.dumps (jsonobject,sort_keys= true,indent=4)
Show:

[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 Pyth On
python_object = Json.read (json_object)
3.4,simplejson Module Example:
Import Simplejson
# converting Python to JSON
json_object = Simplejson.dumps (python_object)
# converting JS On to Python
python_object = simplejson.loads (json_object)

The json_object can also be file names such as file ("Tmp/table.json")

analysis of 4,json data

Suppose for the Data.json file 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 ': ' B50070100007003 ', ' Responsibilityperson ': ' \xe5\xbc\xa0\xe4\ xb9\x8b\xe8\xaf\x9a ', ' devicemodel ': ' PowerEdge 1950 ', ' Servicetag ': ' 729hh2x ', ' IP ': ' 172.16.20.163 ', ' hostname ': ' Hzshterm1.alibaba.com ', ' manageip ': ' 172.31.58.223 ', ' cabinet ': ' H05 ', ' buytime ': ' 2009-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 the JSON object, and view the type, which is already the dict type.

#test. py
Import Simplejson as json
ddata = json.loads (File ("Data.json"))
print ddata
print type (ddata ) #<type ' Dict ' >
Secondly, we read the key value in the dictionary that corresponds to "data"
>>> ddata[' data '//view dictionary method.

>>>type (ddata[' data '])
<type ' list ' >
Found ddata[' data ' is a list, the list is to be queried by serial number
>>> ddata[' data '][0]//View list method.

>>> type (ddata[' data '][0])
<type ' dict ' >

The number No. 0 element of the ddata[' data ' list is a dictionary.
Okay, so let's check the key for IDC, what's the value?
>>> ddata[' data '][0][' IDC '//View dictionary method.

>>> ddata[' data '][0][' IDC '          //View dictionary method.
' \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 Room











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.