[英]Python json decode and search
I have a python script which gets a JSON file from a URL saves it to a variable called myjson
. 我有一个python脚本,它从URL获取一个JSON文件将其保存到一个名为
myjson
的变量。 The next step I am looking to proceed through the data and select individual fields and output those fields to a file. 我希望下一步继续处理数据并选择单个字段并将这些字段输出到文件中。
The following is an example of the json file i get 以下是我得到的json文件的示例
{
"data":
{"111111111":
{
"date":"Wed Feb 12 17:36:01 UTC 2014",
"left":null,
"right":"test",
"category":"test",
"owner":"test",
"name":"test",
"id":123456789123,
"status":"test",
"severity":"test",
"subject":"test",
},
"111111112":
{
"date":"Wed Feb 12 17:36:01 UTC 2014",
"left":null,
"right":"test",
"category":"test",
"owner":"test",
"name":"test",
"id":123456789123,
"status":"test",
"severity":"test",
"subject":"test",
}
}
Ideally I would like to create a file with the "111111111" field and the content of the "subject":"test" (only the test returned) for each entry in the json data structure 理想情况下,我想创建一个文件,其中包含“111111111”字段和“subject”的内容:“test”(仅返回测试)json数据结构中的每个条目
Decode the JSON with the json
module , then just loop over the keys and values of the data
dictionary; 使用
json
模块解码JSON,然后循环遍历data
字典的键和值; that's: 那是:
import json
json_data = json.loads(myjson)
for key, entry in json_data['data'].iteritems():
print key, entry['subject']
If you are using Python 3, use myjson['data'].items():
instead. 如果您使用的是Python 3,请使用
myjson['data'].items():
而不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.