如何解析JSON中的数据? [英] How to parse data in JSON?

查看:114
本文介绍了如何解析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
j = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print j['two']

这篇关于如何解析JSON中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆