-1

我在解析获取 URL 时获得的 JSON 对象时遇到一些问题:

[{"id":1,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8},{"id":2,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8}]

就像您可以看到 JSON 以“[”开头并以“]”结尾

我正在使用这段代码:

import json
import urllib2

data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
print data

我明白了:

[{u'severity': 12, u'title': u'title', u'url': u'https://example.com/432', u'external_id': u'2312', u'notes': u'notes', u'abstract': u'dsadasdas', u'other': [u'aaaaaaaaaaaaaaaaaa', u'bbbbbbbbbbbbbb', u'cccccccccccccccc', u'dddddddddddddd', u'eeeeeeeeee'], u'thing': u'32132', u'version': 23, u'nana': 8, u'details': u'something', u'date': u'2', u'reliability': 0, u'type': u'typeA', u'id': 1, u'accuracy': 0}, {u'severity': 12, u'title': u'title', u'url': u'https://example.com/432', u'external_id': u'2312', u'notes': u'notes', u'abstract': u'dsadasdas', u'other': [u'aaaaaaaaaaaaaaaaaa', u'bbbbbbbbbbbbbb', u'cccccccccccccccc', u'dddddddddddddd', u'eeeeeeeeee'], u'thing': u'32132', u'version': 23, u'nana': 8, u'details': u'something', u'date': u'2', u'reliability': 0, u'type': u'typeA', u'id': 2, u'accuracy': 0}]

如果 JSON 太大,我不会得到完整的信息。

我做错了什么?

谢谢

4

1 回答 1

1

[]在 json中没有任何问题。它只是意味着一个列表。要漂亮地打印您的 json,请尝试以下操作:

import json
import urllib2

data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

要查找特定对象,只需执行以下操作:

obj = next((obj for obj in data if obj["id"] == 2), None)
于 2013-10-10T18:36:19.520 回答