Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中为根元素解析Json_Python_Json_Python 3.x - Fatal编程技术网

在Python中为根元素解析Json

在Python中为根元素解析Json,python,json,python-3.x,Python,Json,Python 3.x,这是我需要解析的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" : {

这是我需要解析的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”的值。我已经使用了Python,但它不起作用。如果我使用
*
整个响应都会打印出来。看起来整个响应都是root。我在上使用了不同的组合,例如
*.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()])

您没有基/根元素,只有键和值的集合

您可以通过如下方式对其进行迭代:

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
):


您似乎没有根id元素,只有子元素中的id。我尝试了这个,但我希望一次只有一个值,不知道实用程序JSON_路径有多有用。我如何获得一个“id”,使用上面的代码可以同时提供两个id,这很好,但我如何一次拥有一个值。
d.keys()
返回一个类似列表的对象,该对象支持成员资格测试和迭代等操作,因此如果要使用以下代码访问任何元素:
d.keys()[1]
以获取第一个“id”
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