-1

我知道要问这种微不足道的事情……但我是 python 的新手。这是一个json字符串

reply = {u'f': [{u'v': u'0'}]}

如何使用 python 从中解析出值 0。我试过了

count = reply['rows'][0]['v']

但它不工作

4

2 回答 2

3

count = reply['f'][0]['v']我相信应该工作。

reply是一本字典。因此,您需要使用字典键来访问数据。在这种情况下,关键是'f',而不是'rows'

于 2012-11-29T19:02:57.200 回答
0

如果你有有效的 JSON,你可以使用 simplejson 模块:

from simplejson import loads, dumps

my_dict = loads(my_json_serialized_string)

然后你可以访问python dict,例如:

print my_dict.items()
print my_dict.keys()
print my_dict.values()

#lets assume 'rows' exists as a key, and the value is a list, and the first item of that list is a dict that contains the key 'v':
print my_dict['rows'][0]['v']

您甚至可以更改 dict,并将其序列化为有效的 JSON 字符串:

my_dict['my_key'] = 'my_value'

my_other_json_serialized_string = dumps(my_dict)
于 2012-11-30T16:14:30.857 回答