如何从一个文件中提取多个JSON对象? [英] How to extract multiple JSON objects from one file?
本文介绍了如何从一个文件中提取多个JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Json文件非常陌生.如果我有一个包含多个json对象的json文件,例如:
I am very new to Json files. If I have a json file with multiple json objects such as following:
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
"Code":[{"event1":"A","result":"1"},…]}
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
"Code":[{"event1":"B","result":"1"},…]}
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
"Code":[{"event1":"B","result":"0"},…]}
…
我想将所有时间戳记"和有用性"提取到数据帧中:
I want to extract all "Timestamp" and "Usefulness" into a data frames:
Timestamp Usefulness
0 20140101 Yes
1 20140102 No
2 20140103 No
…
有人知道解决此类问题的一般方法吗?
Does anyone know a general way to deal with such problems?
推荐答案
使用json数组,格式为:
Use a json array, in the format:
[
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
"Code":[{"event1":"A","result":"1"},…]},
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
"Code":[{"event1":"B","result":"1"},…]},
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
"Code":[{"event1":"B","result":"0"},…]},
...
]
然后将其导入到您的python代码中
Then import it into your python code
import json
with open('file.json') as json_file:
data = json.load(json_file)
现在,数据的内容是一个数组,其中字典代表每个元素.
Now the content of data is an array with dictionaries representing each of the elements.
您可以轻松访问它,即:
You can access it easily, i.e:
data[0]["ID"]
这篇关于如何从一个文件中提取多个JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文