为根元素解析 Python 中的 Json
Parsing Json in Python for root element
这是我需要解析的 JSON 字符串。
{
"192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1" : {
"@class" : "com.barco.compose.media.transcode.internal.h264.H264Gateway",
"id" : "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1",
"uri" : {
"high" : "rtp://239.1.1.2:5006",
"low" : "rtp://239.1.1.1:5006"
},
"owner" : {
"@class" : "com.barco.compose.media.transcode.Acma",
"source" : "dvi1-1-mna-1890322558",
"stream" : "udp://239.1.1.7:5004",
"type" : "video",
"resolution" : [ 1, 1 ],
"framerateDivider" : [ 1, 1 ],
"profile" : [ "baseline" ],
"output" : [ "high", "low" ],
"destination" : [ "", "" ],
"ipvsProfile" : "high",
"ipvsSDP" : [ "" ],
"ipvsTitle" : "DiORStream",
"ipvsTagName" : [ "" ],
"ipvsTagValue" : [ "" ],
"ipvsDescription" : "NMSDesc",
"ipvsHLS" : true
},
"type" : "video"
},
"192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2" : {
"@class" : "com.barco.compose.media.transcode.internal.h264.H264Gateway",
"id" : "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2",
"uri" : {
"high" : "rtp://239.1.1.4:5006",
"low" : "rtp://239.1.1.3:5006"
},
"owner" : {
"@class" : "com.barco.compose.media.transcode.Acma",
"source" : "dvi1-1-mna-1890322558",
"stream" : "udp://239.1.1.7:5004",
"type" : "video",
"resolution" : [ 1, 1 ],
"framerateDivider" : [ 1, 1 ],
"profile" : [ "baseline" ],
"output" : [ "high", "low" ],
"destination" : [ "", "" ],
"ipvsProfile" : "high",
"ipvsSDP" : [ "" ],
"ipvsTitle" : "nikhil",
"ipvsTagName" : [ "" ],
"ipvsTagValue" : [ "" ],
"ipvsDescription" : "nikhilDesc",
"ipvsHLS" : true
},
"type" : "video"
}
}
现在我想获取 "id" 的值。我用过jsonpath-rw library for Python, but that is not working. If I use *
whole response gets printed. Looks like the whole response is root. I have used different combinations on http://jsonpath.curiousconcept.com/,比如*.id
,$[0].id
。
下面的表达式就是你所需要的:
$..id
证明:
In [12]: vv = json.loads(my_input_string)
In [13]: jsonpath_expr = parse('$..id')
In [14]: [x.value for x in jsonpath_expr.find(vv)]
Out[14]:
[u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1',
u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2']
但是,请注意,在使用 ..
运算符时,因为它会进行深度扫描。这意味着如果您的 JSON 中有任何其他对象带有 id
字段,该字段的值也将添加到查询结果中。
您的 JSON
对象包含多个根(或顶部)对象,这些对象使用 "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1"
等键进行索引。根据您的问题,您似乎想要访问这些元素的 id
字段。为此,简单的方法可能是使用标准库中包含的 json
模块来加载字符串,然后访问每个元素的 id
。
import json
my_json_string = "..."
my_json_dict = json.loads(my_json_string)
for key, value in my_json_dict.items():
print("Id is {} for item {}".format(value["id"], key))
然而,如果您只想要 JSON
对象的键,您只需要
import json
my_json_string = "..."
my_json_dict = json.loads(my_json_string)
print(["Got item with id '{}'".format(key) for key in d.keys()])
您没有 base/root 元素 - 只有键和值的集合。
您可以像这样遍历它们:
import json
import pprint
values = json.loads(YOUR_JSON_STRING)
for v in values:
print v, pprint.pprint(values[v])
这将在生成的 JSON 对象中打印出以下两个元素(每个元素都有自己的键):
192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1{u'@class': u'com.barco.compose.media.transcode.internal.h264.H264Gateway',
u'id': u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1',
u'owner': {u'@class': u'com.barco.compose.media.transcode.Acma',
u'destination': [u'', u''],
u'framerateDivider': [1, 1],
u'ipvsDescription': u'NMSDesc',
u'ipvsHLS': True,
u'ipvsProfile': u'high',
u'ipvsSDP': [u''],
u'ipvsTagName': [u''],
u'ipvsTagValue': [u''],
u'ipvsTitle': u'DiORStream',
u'output': [u'high', u'low'],
u'profile': [u'baseline'],
u'resolution': [1, 1],
u'source': u'dvi1-1-mna-1890322558',
u'stream': u'udp://239.1.1.7:5004',
u'type': u'video'},
u'type': u'video',
u'uri': {u'high': u'rtp://239.1.1.2:5006', u'low': u'rtp://239.1.1.1:5006'}}
None
192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2{u'@class': u'com.barco.compose.media.transcode.internal.h264.H264Gateway',
u'id': u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2',
u'owner': {u'@class': u'com.barco.compose.media.transcode.Acma',
u'destination': [u'', u''],
u'framerateDivider': [1, 1],
u'ipvsDescription': u'nikhilDesc',
u'ipvsHLS': True,
u'ipvsProfile': u'high',
u'ipvsSDP': [u''],
u'ipvsTagName': [u''],
u'ipvsTagValue': [u''],
u'ipvsTitle': u'nikhil',
u'output': [u'high', u'low'],
u'profile': [u'baseline'],
u'resolution': [1, 1],
u'source': u'dvi1-1-mna-1890322558',
u'stream': u'udp://239.1.1.7:5004',
u'type': u'video'},
u'type': u'video',
u'uri': {u'high': u'rtp://239.1.1.4:5006', u'low': u'rtp://239.1.1.3:5006'}}
你可以这样得到 id
(将你的 json 字符串保存为 单行 文件 test.json
):
import simplejson as json
with open("test.json") as fp:
line = fp.readline()
json = json.loads(line)
for k,v in json.items():
print v.get("id","no id found") # no id found is the default value in case there is no id defined
这是我需要解析的 JSON 字符串。
{
"192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1" : {
"@class" : "com.barco.compose.media.transcode.internal.h264.H264Gateway",
"id" : "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1",
"uri" : {
"high" : "rtp://239.1.1.2:5006",
"low" : "rtp://239.1.1.1:5006"
},
"owner" : {
"@class" : "com.barco.compose.media.transcode.Acma",
"source" : "dvi1-1-mna-1890322558",
"stream" : "udp://239.1.1.7:5004",
"type" : "video",
"resolution" : [ 1, 1 ],
"framerateDivider" : [ 1, 1 ],
"profile" : [ "baseline" ],
"output" : [ "high", "low" ],
"destination" : [ "", "" ],
"ipvsProfile" : "high",
"ipvsSDP" : [ "" ],
"ipvsTitle" : "DiORStream",
"ipvsTagName" : [ "" ],
"ipvsTagValue" : [ "" ],
"ipvsDescription" : "NMSDesc",
"ipvsHLS" : true
},
"type" : "video"
},
"192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2" : {
"@class" : "com.barco.compose.media.transcode.internal.h264.H264Gateway",
"id" : "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2",
"uri" : {
"high" : "rtp://239.1.1.4:5006",
"low" : "rtp://239.1.1.3:5006"
},
"owner" : {
"@class" : "com.barco.compose.media.transcode.Acma",
"source" : "dvi1-1-mna-1890322558",
"stream" : "udp://239.1.1.7:5004",
"type" : "video",
"resolution" : [ 1, 1 ],
"framerateDivider" : [ 1, 1 ],
"profile" : [ "baseline" ],
"output" : [ "high", "low" ],
"destination" : [ "", "" ],
"ipvsProfile" : "high",
"ipvsSDP" : [ "" ],
"ipvsTitle" : "nikhil",
"ipvsTagName" : [ "" ],
"ipvsTagValue" : [ "" ],
"ipvsDescription" : "nikhilDesc",
"ipvsHLS" : true
},
"type" : "video"
}
}
现在我想获取 "id" 的值。我用过jsonpath-rw library for Python, but that is not working. If I use *
whole response gets printed. Looks like the whole response is root. I have used different combinations on http://jsonpath.curiousconcept.com/,比如*.id
,$[0].id
。
下面的表达式就是你所需要的:
$..id
证明:
In [12]: vv = json.loads(my_input_string)
In [13]: jsonpath_expr = parse('$..id')
In [14]: [x.value for x in jsonpath_expr.find(vv)]
Out[14]:
[u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1',
u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2']
但是,请注意,在使用 ..
运算符时,因为它会进行深度扫描。这意味着如果您的 JSON 中有任何其他对象带有 id
字段,该字段的值也将添加到查询结果中。
您的 JSON
对象包含多个根(或顶部)对象,这些对象使用 "192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1"
等键进行索引。根据您的问题,您似乎想要访问这些元素的 id
字段。为此,简单的方法可能是使用标准库中包含的 json
模块来加载字符串,然后访问每个元素的 id
。
import json
my_json_string = "..."
my_json_dict = json.loads(my_json_string)
for key, value in my_json_dict.items():
print("Id is {} for item {}".format(value["id"], key))
然而,如果您只想要 JSON
对象的键,您只需要
import json
my_json_string = "..."
my_json_dict = json.loads(my_json_string)
print(["Got item with id '{}'".format(key) for key in d.keys()])
您没有 base/root 元素 - 只有键和值的集合。
您可以像这样遍历它们:
import json
import pprint
values = json.loads(YOUR_JSON_STRING)
for v in values:
print v, pprint.pprint(values[v])
这将在生成的 JSON 对象中打印出以下两个元素(每个元素都有自己的键):
192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1{u'@class': u'com.barco.compose.media.transcode.internal.h264.H264Gateway',
u'id': u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream1',
u'owner': {u'@class': u'com.barco.compose.media.transcode.Acma',
u'destination': [u'', u''],
u'framerateDivider': [1, 1],
u'ipvsDescription': u'NMSDesc',
u'ipvsHLS': True,
u'ipvsProfile': u'high',
u'ipvsSDP': [u''],
u'ipvsTagName': [u''],
u'ipvsTagValue': [u''],
u'ipvsTitle': u'DiORStream',
u'output': [u'high', u'low'],
u'profile': [u'baseline'],
u'resolution': [1, 1],
u'source': u'dvi1-1-mna-1890322558',
u'stream': u'udp://239.1.1.7:5004',
u'type': u'video'},
u'type': u'video',
u'uri': {u'high': u'rtp://239.1.1.2:5006', u'low': u'rtp://239.1.1.1:5006'}}
None
192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2{u'@class': u'com.barco.compose.media.transcode.internal.h264.H264Gateway',
u'id': u'192.168.1.2_151b3a32-ce00-114a-e000-0004a50c1c6d_stream2',
u'owner': {u'@class': u'com.barco.compose.media.transcode.Acma',
u'destination': [u'', u''],
u'framerateDivider': [1, 1],
u'ipvsDescription': u'nikhilDesc',
u'ipvsHLS': True,
u'ipvsProfile': u'high',
u'ipvsSDP': [u''],
u'ipvsTagName': [u''],
u'ipvsTagValue': [u''],
u'ipvsTitle': u'nikhil',
u'output': [u'high', u'low'],
u'profile': [u'baseline'],
u'resolution': [1, 1],
u'source': u'dvi1-1-mna-1890322558',
u'stream': u'udp://239.1.1.7:5004',
u'type': u'video'},
u'type': u'video',
u'uri': {u'high': u'rtp://239.1.1.4:5006', u'low': u'rtp://239.1.1.3:5006'}}
你可以这样得到 id
(将你的 json 字符串保存为 单行 文件 test.json
):
import simplejson as json
with open("test.json") as fp:
line = fp.readline()
json = json.loads(line)
for k,v in json.items():
print v.get("id","no id found") # no id found is the default value in case there is no id defined