【问题标题】:Format Json response, select few keys / objects and list the selected objects [duplicate]格式化Json响应,选择几个键/对象并列出所选对象[重复]
【发布时间】:2018-02-15 07:10:45
【问题描述】:

我已经开始使用 python,我正在编写一个代码来获取 http json 响应,从我想要过滤的 json 响应中,选择键/对象并打印它。我能够得到以下格式的 json 响应

{'name': 'M985533026-14742', 'instanceAttributes': '{"orgName":"0a1e554f-2d27-4516-811e-1ed74b192cdf","sessionUri":"://us-virginia-1-12.vchs.vmware.com/api/compute/api/sessions"," apiVersionUri":"://us-virginia-1-12.vchs.vmware.com/api/compute/api/versions"}', “仪表板网址”: '://us-virginia-1-12.vchs.vmware.com/api/compute/compute/ui/index_dr.html?orgName=0a1e554f-2d27-4516-811e-1ed74b192cdf&serviceInstanceId=c2343357-9578-4c38-85c9- 4450ce08cda3&servicePlan=67538ff0-f4c3-48cb-8a6f-b0a3ac5aa324', '描述':'M985533026-14742','apiUrl': '://us-virginia-1-12.vchs.vmware.com/api/compute/api/org/c2343357-9578-4c38-85c9-4450ce08cda3', 'instanceVersion':'1.0','id': 'c2343357-9578-4c38-85c9-4450ce08cda3','链接':[],'serviceGroupId': '5a7e5ae7-a6bc-4226-a5b6-844e7dd8c88e','区域': 'us-virginia-1-12.vchs.vmware.com'、'planId': '区域:us-virginia-1-12.vchs.vmware.com:planID:67538ff0-f4c3-48cb-8a6f-b0a3ac5aa324'}, {'name': '虚拟私有云 OnDemand', 'instanceAttributes': '{"orgName":"0e95ddcd-f1d8-4950-aaab-01c2b713f61f","sessionUri":"://us-california-1-3.vchs.vmware.com/api/compute/api/sessions"," apiVersionUri":"://us-california-1-3.vchs.vmware.com/api/compute/api/versions"}', 'dashboardUrl':

现在我只需要过滤区域和 orgName,请帮帮我?

【问题讨论】:

  • 我们不会为您编写代码,但如果您自己的代码以您不理解的方式失败,或者您发现某些文档令人困惑或有其他特定的技术问题我们很乐意提供帮助。
  • 您找到解决方案了吗?

标签: python arrays json http filtering


【解决方案1】:

JSON 对象是一个字典。您可以访问:

res = yourjson['key']

如果 res 也是字典,那么:

final_res = res['other_key']

希望对你有帮助

【讨论】:

    【解决方案2】:

    像字典一样访问 JSON:

    if 'key' in json_object:
         result = json_object['key']
    

    orgName 的情况下,您必须编写如下内容:

    json_object['instanceAttributes'][orgName]
    

    因为是另一个字典中的字典。

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2022-11-24
      • 2015-06-19
      • 1970-01-01
      相关资源
      最近更新 更多