如何在Python中解析JSON?

如何在Python中解析JSON?

问题描述:

我的项目当前正在python中接收一条JSON消息,我需要从中获取一些信息.为此,我们将其设置为字符串中的一些简单JSON:

My project is currently receiving a JSON message in python which I need to get bits of information out of. For the purposes of this, let's set it to some simple JSON in a string:

jsonStr = '{"one" : "1", "two" : "2", "three" : "3"}'

到目前为止,我一直在使用列表然后是json.dumps来生成JSON请求,但与此相反,我认为我需要使用json.loads.但是我没有那么幸运.在上面的示例中,有人可以给我提供一个片段,该片段返回"2"并输入"two"吗?

So far I've been generating JSON requests using a list and then json.dumps, but to do the opposite of this I think I need to use json.loads. However I haven't had much luck with it. Could anyone provide me a snippet that would return "2" with the input of "two" in the above example?

非常简单:

import json
data = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print data['two']