【发布时间】:2014-06-10 00:17:40
【问题描述】:
我在具有嵌套循环的 Json 中有以下数据。这里作者有一个指向className_User的指针。
{ "results": [
{
"accepted": "YES",
"author": {
"__type": "Pointer",
"className": "_User",
"objectId": "HZ1c6AmfuE"
},
"createdAt": "2014-02-19T22:39:47.899Z",
"objectId": "t6G5lLaxoR",
"recipient": {
"__type": "Pointer",
"className": "_User",
"objectId": "avIzy6RtKQ"
},
"updatedAt": "2014-02-19T22:40:08.906Z"
},
{
"accepted": "YES",
"author": {
"__type": "Pointer",
"className": "_User",
"objectId": "HZ1c6AmfuE"
},
"createdAt": "2014-02-20T00:18:40.640Z",
"objectId": "ZmagKJJVKd",
"recipient": {
"__type": "Pointer",
"className": "_User",
"objectId": "zw48k1oAw3"
},
"updatedAt": "2014-02-20T00:19:17.602Z"
}] }
我使用以下代码读取文件
json_data = pd.read_json('RelationshipRequest.json')
df = pd.concat([pd.DataFrame.from_dict(item, orient='index').T for item in json_data.results])
print df.author
这打印:
0 {u'className': u'_User', u'__type': u'Pointer'...
0 {u'className': u'_User', u'__type': u'Pointer'...
有没有一种方法可以从作者的嵌套字段中提取 objectId 以获取 author.objectId = HZ1c6AmfuE ?
【问题讨论】:
-
您不使用
json.load(filename)或json.loads(json_string)然后只是使用parsed_json['author']['objectid']的任何特殊原因? -
您发布的数据不是有效的 JSON 格式。我尝试使用
json.load加载它,但失败了。 -
json.load 导致“AttributeError: 'str' object has no attribute 'read'”
-
@HaiVu 成功了,
>>> json.loads("""{...}""")给了{'results': [... -
我的错:我复制/粘贴并错过了最后一行。