简体   繁体   中英

How to print specific json list items?

I'm making a python bot and the response is coming back as JSON.

Here is a quick show of what it brings back:

[["Message","User string from here."]]

So first what I've done is, loaded the json from the python module json .

json.loads(resp)

and it brings back:

[[u"Message",u"user string from here"]]

How do I print out the Message which will return the value user string from here ?

All you need to do is:

from __future__ import print_function

import json

raw_response = '[["Message","User string from here."]]'
data = json.loads(raw_response)

print(data[0][1])

json.loads converts json into the native datatypes of python. So, when you did json.loads(resp) , you get back a list of lists.

You can simply iterate over the list to access whatever elements you want.

In your case, print json.loads(resp)[0][1] should suffice.

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