在下面的JSON中,我想访问每个用户的email id和“gamesplayed”字段。在
"UserTable" : {
"abcd@gmailcom" : {
"gameHistory" : {
"G1" : [ {
"category" : "1",
"questiontext" : "What is the cube of 2 ?"
}, {
"category" : "2",
"questiontext" : "What is the cube of 4 ?"
} ]
},
"gamesplayed" : 2
},
"xyz@gmailcom" : {
"gameHistory" : {
"G1" : [ {
"category" : "1",
"questiontext" : "What is the cube of 2 ?"
}, {
"category" : "2",
"questiontext" : "What is the cube of 4 ?"
} ]
},
"gamesplayed" : 2
}
}
以下是我用来尝试访问用户电子邮件id的代码:
^{pr2}$这是我得到的错误:
File "C:\ProgramData\Anaconda3\lib\site-packages\jsonpath_rw\jsonpath.py", line 444, in find
return [DatumInContext(datum.value[self.index], path=self, context=datum)]
KeyError: 0
当我运行下面的行并访问每个用户的“gamesplayed”字段时,IDE崩溃了。在
print (parser.ExtentedJsonPathParser().parse("$.*.gamesplayed").find(gd_info))
如果您喜欢使用JSONPath。请试试这个。在
Python代码:
输出:
^{pr2}$Python可以将有效的json作为字典处理。因此,您必须将json字符串解析为python字典。
现在可以通过使用特定键作为索引
^{pr2}$value = dict[key]
来访问值。相关问题 更多 >
编程相关推荐