Python-json Module

Source: Internet
Author: User

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 for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMAA subset of -262 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. 2, the format of the JSON2.1, object: [plain] View plaincopy {name:"Peggy", Email:"[email protected]", homepage:"http://www.peggy.com"} {property: Value, Property: Value, property: Value}2.2, array: is a collection of sequential values. An array starts at"[", ending in"]", the value is used between","separated. [Plain] View plaincopy [{name:"Peggy", Email:"[email protected]", homepage:"http://www.peggy.com"}, {name:"Peggy", Email:"[Email Protected]om", homepage:"http://www.peggy.com"}, {name:"Peggy", Email:"[email protected]", homepage:"http://www.peggy.com"}] Also, the value can be a string, a number, true, false, NULL, or an object or an array. These structures can be nested. 3, the JSON import is exported here to the write/the meaning of dump is to input a JSON object into a python_object, and if Python_object is a file, dump it into a file, or dump it into memory if it is an object. This is serialization. 3.1, reading the JSON file [python] view plaincopyImportSimplejson as JSON f= File ('Table.json') Source=F.read () Target=JSON. Jsondecoder (). Decode (source)PrintTarget [python] view plaincopyImportSimplejson as JSON jsonobject= Json.load (File ('Table.json'))  PrintJsonobject3.2, show JSON file in order to show good looking JSON format, original JSON file: [python] View plaincopy [[email protected]] $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: [python] View plaincopyImportSimplejson as JSON jsonobject= Json.load (File ('Table.json'))  PrintJson.dumps (jsonobject,sort_keys=true,indent=4) display: [Python] View plaincopy [[email protected]] $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: [Python] View plaincopyImportJSON#converting Python to JSONJson_object =json.write (python_object)#converting JSON to PythonPython_object =Json.read (json_object)3.4, Simplejson module example: [Python] View plaincopyImportSimplejson#converting Python to JSONJson_object =simplejson.dumps (python_object)#converting JSON to PythonPython_object =simplejson.loads (json_object) Json_object can also be a file name such as file ("TMP/Table.json ")4, the parsing of JSON data assumes the following for the Data.json file: [plain] View Plaincopy {'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, build the JSON object, and look at the type, which is already the dict type. [Python] View plaincopy#test.pyImportSimplejson as JSON ddata= Json.loads (File ("Data.json"))  PrintDdataPrintType (Ddata)#<type ' dict ' >Secondly, we read the key in the dictionary as the "data" corresponding to the value>>> ddata['Data']    //How to view the dictionary! [Python] View plaincopy>>>type (ddata['Data'])  <type'List'>discover that ddata[' data ' is a list, and the list will be queried by ordinal>>> ddata['Data'][0]//How to view the list! [Python] View plaincopy>>> 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.>>> ddata['Data'][0]['IDC']          //How to view the dictionary! [Python] View plaincopy>>> ddata['Data'][0]['IDC']          //How to view the dictionary! '\XE6\X9D\XAD\XE5\XB7\X9E\XE5\XBE\XB7\XE8\X83\X9C\XE6\X9C\XBA\XE6\X88\XBF'>>>Printddata['Data'][0]['ID

Python-json Module

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.