从JSON文件parsing值?

我有一个文件中的这个JSON:

{ "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": [ "id": "valore" ], "om_points": "value", "parameters": [ "id": "valore" ] } 

我写了这个脚本打印所有的JSON文本:

 json_data=open(file_directory).read() data = json.loads(json_data) pprint(data) 

我如何parsing文件并提取单个值?

我想伊格纳西奥说的是你的JSON文件是不正确的。 你应该有{}[]用于列表, {}用于字典。

以下是您的JSON文件的外观,您的JSON文件甚至不会加载我:

 { "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": { "id": "valore" }, "om_points": "value", "parameters": { "id": "valore" } } 

那么你可以使用你的代码:

 import json from pprint import pprint data = json.load(open('data.json')) pprint(data) 

有了数据,你现在也可以find像这样的值:

 data["maps"][0]["id"] data["masks"]["id"] data["om_points"] 

尝试一下,看看它是否开始有意义。

你的data.json应该是这样的:

 { "maps":[ {"id":"blabla","iscategorical":"0"}, {"id":"blabla","iscategorical":"0"} ], "masks": {"id":"valore"}, "om_points":"value", "parameters": {"id":"valore"} } 

你的代码应该是:

 import json from pprint import pprint with open('data.json') as data_file: data = json.load(data_file) pprint(data) 

请注意,这只适用于Python 2.6及更高版本,因为它取决于with -statement 。 在Python 2.5中使用from __future__ import with_statement ,在Python <= 2.4中,请参阅Justin Peel的答案 ,这个答案是基于这个答案的。

您现在也可以像这样访问单个值:

 data["maps"][0]["id"] # will return 'blabla' data["masks"]["id"] # will return 'valore' data["om_points"] # will return 'value' 
 data = [] with codecs.open('d:\output.txt','rU','utf-8') as f: for line in f: data.append(json.loads(line)) 

@Justin Peel的答案是非常有帮助的,但是如果你使用Python 3阅读JSON,应该这样做:

 with open('data.json', encoding='utf-8') as data_file: data = json.loads(data_file.read()) 

注意:使用json.loads而不是json.load 。 在Python 3中, json.loads接受一个string参数。 json.load采用类似文件的对象参数。 data_file.read()返回一个string对象。

“超JSON”或者简单的“ujson”可以在你的JSON文件input中处理[] 。 如果您正在将JSONinput文件作为JSON元素列表读入程序, 例如[{[{}]}, {}, [], etc...] ujson可以处理任何字典列表的字典列表的任意顺序。

您可以在Python包索引中findujson,并且API几乎与Python的内置json库相同。

如果您加载更大的JSON文件,则ujson速度也会更快。 您可以在提供的相同链接中看到与其他Python JSON库相比较的性能细节。

如果你在python 3这里是你如何做到这一点

 { "connection1": { "DSN": "con1", "UID": "abc", "PWD": "1234", "connection_string_python":"test1" } , "connection2": { "DSN": "con2", "UID": "def", "PWD": "1234" } } 

代码应该看起来像假设connection.json文件看起来像上面

 connection_file = open('connection.json', 'r') conn_string = json.load(connection_file) conn_string['connection1']['connection_string_python']) >>>test1 
  # Here you go with modified json file: # data.json file : { "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": [{ "id": "valore" }], "om_points": "value", "parameters": [{ "id": "valore" }] } # You can call or print data on console by using below lines import json from pprint import pprint with open('data.json') as data_file: data_item = json.load(data_file) pprint(data_item) print(data_item['parameters'][0]['id']) #Output : #pprint(data_item) output as : {'maps': [{'id': 'blabla', 'iscategorical': '0'}, {'id': 'blabla', 'iscategorical': '0'}], 'masks': [{'id': 'valore'}], 'om_points': 'value', 'parameters': [{'id': 'valore'}]} #print(data_item['parameters'][0]['id']) output as : valore