我有这样的结构:
[{
"meta": {
"foo": 1,
"bar": "string",
"baz": "string2"},
"data": [
{"id": "1", "quant": 2, "price": 3.14},
{"id": "2", "quant": 1, "price": 6.66}
]
},
...
]
如何从元数据中获取元素foo,bar,从数据中获取价格?
答案 0 :(得分:2)
欢迎来到SO,我们不是代码编写服务,所以请在下次提问时发布您的工作。有关python Data Sructures.
的更多信息,请参阅文档 通过索引访问 lists
,并通过`keys'
Dictionaries
与_list[index]
和_dict['key']
一样,有了这些基础知识,我们就可以继续前进。
l= [{ "meta":{ "foo":1, "bar":"string", "baz":"string2"}, "data":[ {"id":"1", "quant":2, "price":3.14}, {"id":"2", "quant":1, "price":6.66}] } ]
>>> l[0]['meta']['foo']
1
>>> l[0]['meta']['bar']
'string'
>>> l[0]['data'][0]['price']
3.14
>>> l[0]['data'][0]['quant']
2
>>>
>>> l[0]['data'][1]['quant']
1
>>> l[0]['data'][1]['price']
6.66
>>>