Python parsing JSON string

Source: Internet
Author: User

Problem analysis

Parsing a JSON string and extracting its key and value out of the list in public, first had to parse the JSON format, the following is an individual analysis process:

Source
Def read (Obj,key):     collect = list ()     for k  in obj:        v = obj[k]         if isinstance (V,STR)  or isinstance (V,unicode):             if key== ':                 collect.append ({k:v})              else:                 collect.append ({str (key) + ".") +K:V})         elif isinstance (V,int):             if key== ':                 collect.append ({k:v})             else:                 collect.append ({str (key) + ".") +K:V})         elif isinstance (V,bool):             if key== ':                 collect.append ({k:v})              else:                 collect.append ({str (key) + ".") +K:V})         elif isinstance (v,dict):             collect.extend (Read (v,k))          elif isinstance (v,list):          &nbsP;  collect.extend (ReadList (v,key))     return collect     def readlist (Obj,key):     collect = list ()     for  index,item in enumerate (obj):        for k  IN ITEM:            V = ITEM[K]             if isinstance (V,STR)  or  isinstance (V,unicode):                 collect.append ({key+ "[" +STR (Index) + "]" + "." +K:V})             elif isinstance (V,int):                 collect.append ({key+ "[" +STR (Index) + "]" + "." +K:V})             elif isinstance (V,bool):                 collect.append ({key+ "[" +STR (Index) + "]" + "." +K:V})             elif isinstance (v,dict):                 collect.extend (Read (v,key+ "[" +STR (Index) + "]"))             elif  isinstance (v,list):                 collect.extend (ReadList (v,key+ "[" +STR (Index) + "]"))     return collect
Example
Import jsonjsonstr = ' {' num ': 1, ' Boo ': true, ' obj ': {' sex ': ' Boy ', ' Age ': ' "result ': [{' Name ': ' SaaS ', ' Leverl ': [{' Name '] : "English", "Mark": "the"), "id": "336efba7-259d-4057-ae1e-679e81419cd7", "module": "Life"}]} ' OJT = Json.loads (JSONSTR) Print read (OJT, ') #输出结果 [{u ' num ': 1}, {u ' obj.age ': $}, {u ' obj.sex ': U ' Boy '}, {u ' [0].module ': U ' Life '}, {u ' [0].name ': U ' SaaS '}, {u ' [0][0].name ': U ' English '}, {u ' [0][0].mark ': ', '}, {u ' [0].id ': U ' 336efba7-259d-4057-ae1e-679e81419cd7 '}, {u ' Boo ': True}]


Python parsing JSON string

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