在Python中解析一个寻找特定值的通用JSON结构

时间:2023-02-09 21:14:57

I'm trying to parse a JSON structure and looking for specific values.

我正在尝试解析JSON结构并查找特定值。

Here an example of a possibile data to parse:

这里是一个可解析的可能数据的示例:

{
    "objects": [
        {
            "from": 44,
            "my_ref": "http://192.168.10.1/index.php",
            "allow": "no",
            "to": 10
        },
        {
            "from": 20,
            "my_ref": "http://192.168.10.2/index.php",
            "allow": "mandatory",
            "to": 0
        }
    ],
    "comment": "My PHP",
    "identifiable_with_user": true,
    "key": 10,
    "link": [
        {
            "href": "http://192.168.10.1/index.php",
            "method": "GET",
            "rel": "self",
            "type": "website"
        },
        {
            "href": "http://192.168.10.5/identifiable.php",
            "method": "GET",
            "rel": "account_info"
        }
    ],
    "name": "Accounts",
    "read_only": true,
    "system": true,
    "system_key": 20,
    "tls_match_ref": "http://192.168.10.5/accounts.php"
}

I need to extract all the values identified by the key *ref (like my_ref, href, etc...) in a generic JSON structure. I just need to extract the URL and then perform some actions:

我需要在通用JSON结构中提取由key * ref标识的所有值(如my_ref,href等...)。我只需要提取URL然后执行一些操作:

for entities in JSON_structure 
    URL= "take the URL corresponding to the ref key"
    so something with the URL

I tried using .items() filtering "http://" string but didn't work

我尝试使用.items()过滤“http://”字符串,但没有用

 URL = {k:v for (k,v) in element_detail.items() if "http://" in k or v}

1 个解决方案

#1


0  

You should parse the data then look for the keys with ref in it.

您应该解析数据,然后查找带有ref的键。

That does the trick :

这样做的诀窍:

import json

def parse(root):
  for k,v in root.items():
    if isinstance(v, list):
      for e in v:
        parse(e)
    else:
      if 'ref' in k:
        print(k + ':' + v)


json_data = '{\
  "objects": [\
    { "from": 44, "my_ref": "http://192.168.10.1/index.php", "allow": "no", "to": 10 },\
    { "from": 20, "my_ref": "http://192.168.10.2/index.php", "allow": "mandatory", "to": 0 }\
  ],\
  "comment": "My PHP",\
  "identifiable_with_user": true,\
  "key": 10,\
  "link": [\
    { "href": "http://192.168.10.1/index.php", "method": "GET", "rel": "self", "type": "website" },\
    { "href": "http://192.168.10.5/identifiable.php", "method": "GET", "rel": "account_info" }\
  ],\
  "name": "Accounts",\
  "read_only": true,\
  "system": true,\
  "system_key": 20,\
  "tls_match_ref": "http://192.168.10.5/accounts.php"\
}'

data = json.loads(json_data)
parse(data)

Output :

输出:

href:http://192.168.10.1/index.php
href:http://192.168.10.5/identifiable.php
my_ref:http://192.168.10.1/index.php
my_ref:http://192.168.10.2/index.php
tls_match_ref:http://192.168.10.5/accounts.php

#1


0  

You should parse the data then look for the keys with ref in it.

您应该解析数据,然后查找带有ref的键。

That does the trick :

这样做的诀窍:

import json

def parse(root):
  for k,v in root.items():
    if isinstance(v, list):
      for e in v:
        parse(e)
    else:
      if 'ref' in k:
        print(k + ':' + v)


json_data = '{\
  "objects": [\
    { "from": 44, "my_ref": "http://192.168.10.1/index.php", "allow": "no", "to": 10 },\
    { "from": 20, "my_ref": "http://192.168.10.2/index.php", "allow": "mandatory", "to": 0 }\
  ],\
  "comment": "My PHP",\
  "identifiable_with_user": true,\
  "key": 10,\
  "link": [\
    { "href": "http://192.168.10.1/index.php", "method": "GET", "rel": "self", "type": "website" },\
    { "href": "http://192.168.10.5/identifiable.php", "method": "GET", "rel": "account_info" }\
  ],\
  "name": "Accounts",\
  "read_only": true,\
  "system": true,\
  "system_key": 20,\
  "tls_match_ref": "http://192.168.10.5/accounts.php"\
}'

data = json.loads(json_data)
parse(data)

Output :

输出:

href:http://192.168.10.1/index.php
href:http://192.168.10.5/identifiable.php
my_ref:http://192.168.10.1/index.php
my_ref:http://192.168.10.2/index.php
tls_match_ref:http://192.168.10.5/accounts.php