У меня есть файл JSON со значениями в скобках [], как показано.Я пытаюсь создать словарь [ключ: значение], который показывает [id_value: text_value.]
{"id":6127465, "users":{"name":[{"dr_info":[28,37],"text":"trees"}],"favorites":[]}}
{"id":9285628, "users":{"name":[{"dr_info":[16,24],"text":"grass"}, {"id_info":[30,34],"text":"trees"}],"favorites":[]}}
{"id":7625927, "users":{"name":[{"dr_info":[18,23],"text":"grass"}],"favorites":[], "type" : "photo"}}
{"id":8725946, "users":{"name":[{"dr_info":[23,33],"text":"grass"}, {"id_info":[37,41],"text":"trees"}],"favorites":[]}}
Взяв в качестве примера две первые строки JSON выше.Вывод для словаря будет следующим:
[6127465: «деревья»]
[9285628: «трава», «деревья»] и т. Д.
Вот то, что я кодировал до сих пор, но я не могу получить значения очень хорошо.
dict={}
with open(fileName, 'r') as file_to_read:
for line in file_to_read:
data = json.loads(line)
json_tree = objectpath.Tree(data)
json.dumps(data, ensure_ascii=True)
dict[json_tree.execute('$.id')] = json_tree.execute('$.users.name.text')
return dict
Новое редактирование.(Ответ)
dict={}
with open(fileName, 'r') as file_to_read:
for line in file_to_read:
data = json.loads(line)
json_tree = objectpath.Tree(data)
json.dumps(data)
dict[json_tree.execute('$.id')] = list(json_tree.execute('$.users.name.text'))
return dict