简体   繁体   中英

Creating JSON from list of tuples

I have a list of tuples in the form of:

result=[(0,"string1",4,"string2"),(1,"string3",10,"string4")]

I want it in JSON format of:

one={"id":0,
      "firstname":"string1",
       "age":4}

for each tuple in the list by defining key:value pairs.

Using the json library,

somedict = { "id"     : [ x[0] for x in data ],
            "name" : [ x[1] for x in data ],
            "age" : [ x[2] for x in data ],
           }
print json.dumps(somedict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM