Parsing json and searching through it(解析json并搜索它)
问题描述
我有这个代码
导入json从 pprint 导入 pprintjson_data=open('bookmarks.json')jdata = json.load(json_data)打印(jdata)json_data.close()
如何搜索
u'uri': u'http:?
解决方案由于
<预><代码>>>>jdata = json.load('{"uri": "http:", "foo", "bar"}')>>>'uri' in jdata # 检查 'uri' 是否在 jdata 的键中真的>>>jdata['uri'] # 将返回属于键'uri'的值你'http:'json.loads 只是返回一个字典,您可以使用适用于字典的运算符:
要了解如何遍历数据,请考虑以下示例:
<预><代码>>>>导入json>>>jdata = json.loads(open ('bookmarks.json').read())>>>对于 jdata['children'][0]['children'] 中的 c:...打印'标题:{},URI:{}'.format(c.get('title', 'No title'),c.get('uri', '没有uri'))...标题:最近加入书签,URI:place:folder=BOOKMARKS_MENU(...)标题:最近的标签,URI:place:sort=14&type=6&maxResults=10&queryType=1标题: , URI: 无 uri标题:Mozilla Firefox,URI:无 uri检查
jdata
数据结构将允许您根据需要导航它.您已经拥有的pprint
调用是一个很好的起点.Edit2:又一次尝试.这将获取您在字典列表中提到的文件.有了这个,我认为您应该能够根据自己的需要进行调整.
<预><代码>>>>def build_structure(data, d=[]):...如果数据中的儿童":...对于数据['儿童']中的c:... d.append({'title': c.get('title', 'No title'),... 'uri': c.get('uri', None)})... build_structure(c, d)...返回d...>>>pprint.pprint(build_structure(jdata))[{'title': u'Bookmarks Menu', 'uri': None},{'title':你'最近收藏','uri': u'place:folder=BOOKMARKS_MENU&folder=UNFILED_BOOKMARKS&(...)'},{'title': u'最近的标签','uri': u'place:sort=14&type=6&maxResults=10&queryType=1'},{'title': u'', 'uri': 无},{'title': u'Mozilla Firefox', 'uri': 无},{'title': u'帮助和教程','uri': u'http://www.mozilla.com/en-US/firefox/help/'},(...)}]
然后搜索u'uri': u'http:'
",做这样的事情:
for c in build_structure(jdata):如果 c['uri'].startswith('http:'):打印以 http 开头"
I have this code
import json
from pprint import pprint
json_data=open('bookmarks.json')
jdata = json.load(json_data)
pprint (jdata)
json_data.close()
How can I search through it for u'uri': u'http:
?
As json.loads
simply returns a dict, you can use the operators that apply to dicts:
>>> jdata = json.load('{"uri": "http:", "foo", "bar"}')
>>> 'uri' in jdata # Check if 'uri' is in jdata's keys
True
>>> jdata['uri'] # Will return the value belonging to the key 'uri'
u'http:'
Edit: to give an idea regarding how to loop through the data, consider the following example:
>>> import json
>>> jdata = json.loads(open ('bookmarks.json').read())
>>> for c in jdata['children'][0]['children']:
... print 'Title: {}, URI: {}'.format(c.get('title', 'No title'),
c.get('uri', 'No uri'))
...
Title: Recently Bookmarked, URI: place:folder=BOOKMARKS_MENU(...)
Title: Recent Tags, URI: place:sort=14&type=6&maxResults=10&queryType=1
Title: , URI: No uri
Title: Mozilla Firefox, URI: No uri
Inspecting the jdata
data structure will allow you to navigate it as you wish. The pprint
call you already have is a good starting point for this.
Edit2: Another attempt. This gets the file you mentioned in a list of dictionaries. With this, I think you should be able to adapt it to your needs.
>>> def build_structure(data, d=[]):
... if 'children' in data:
... for c in data['children']:
... d.append({'title': c.get('title', 'No title'),
... 'uri': c.get('uri', None)})
... build_structure(c, d)
... return d
...
>>> pprint.pprint(build_structure(jdata))
[{'title': u'Bookmarks Menu', 'uri': None},
{'title': u'Recently Bookmarked',
'uri': u'place:folder=BOOKMARKS_MENU&folder=UNFILED_BOOKMARKS&(...)'},
{'title': u'Recent Tags',
'uri': u'place:sort=14&type=6&maxResults=10&queryType=1'},
{'title': u'', 'uri': None},
{'title': u'Mozilla Firefox', 'uri': None},
{'title': u'Help and Tutorials',
'uri': u'http://www.mozilla.com/en-US/firefox/help/'},
(...)
}]
To then "search through it for u'uri': u'http:'
", do something like this:
for c in build_structure(jdata):
if c['uri'].startswith('http:'):
print 'Started with http'
这篇关于解析json并搜索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!