首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我有一个列表,其中包含到json中节点的路由。如何访问基于该路由的节点?

我有一个列表,其中包含到json中节点的路由。如何访问基于该路由的节点?
EN

Stack Overflow用户
提问于 2019-08-02 20:13:25
回答 1查看 65关注 0票数 0

我有一个列表,其中包含以下格式的数据。

代码语言:javascript
复制
Data format: "1.3.4.2.1", "1.45.67.32.2", ...(the strings separated by dots 
can be of varying lengths)

这些字符串表示到" status“节点的路由,字符串中的"last index”表示必须分配给status节点中的"@default“属性的值。

我有一个如下格式的json。

代码语言:javascript
复制
json_tree = 
{
  "Gardens": {
    "Seaside": {
      "@loc": "porch",
      "@myID": "1.2.3",
      "Tid": "1",
      "InfoList": {
        "status": {
          "@default": "0",
          "@myID": "26"
        },
        "count": {
          "@default": "0",
          "@myID": "1"
        }
      },
      "BackYard": {
        "@loc": "backyard",
        "@myID": "75",
        "Tid": "2",
        "InfoList": {
          "status": {
            "@default": "6",
            "@myID": "32"
          },
          "count": {
            "@default": "0",
            "@myID": "2"
          }
        }
      }
    }
  }
}

在本例中,我的路由列表包含以下信息。

代码语言:javascript
复制
route_list = ["1.2.3.26.4","1.2.3.75.32.2",...] # this json_tree could have many more layers in the format shown above

Note: '1.2.3.26" is the route to the "status" node in "Gardens" and "4" is the value to be assigned to the "@default" node in the "status".
Note: '1.2.3.75.32" is the route to the "status" node in "BackYard" and "2" is the value to be assigned to the "@default" node in the "status".

到目前为止,我有以下方法。我不能再往前走了。

代码语言:javascript
复制
for item in route_list:
   UpdateJsonTree(json_tree, item)

def UdpateJsonTree(json_tree, item):
   # I am unsure on how to parse the json tree based on the route given
   # and update the '@default' value of the status node 

任何帮助都将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-03 09:00:59

此方法首先构建一个路由主节点,其中包含每个路由的列表条目以进行检查(显示在底部)。

一旦主路由可用,那么路由检查就是直接的。

来自179点答案hereflatten

如果您需要任何澄清,请告诉我。

代码如下:

代码语言:javascript
复制
import _collections_abc
import itertools
from pprint import pprint

l1 = [
    '1.2.3.26.4',
    '1.2.3.75.32.2'
]

json_tree = {
    "Gardens": {
        "Seaside": {
            "@loc": "porch",
            "@myID": "1.2.3",
            "Tid": "1",
            "InfoList": {
                "status": {
                    "@default": "0",
                    "@myID": "26"
                },
                "count": {
                    "@default": "0",
                    "@myID": "1"
                }
            },
            "BackYard": {
                "@loc": "backyard",
                "@myID": "75",
                "Tid": "2",
                "InfoList": {
                    "status": {
                        "@default": "6",
                        "@myID": "32"
                    },
                    "count": {
                        "@default": "0",
                        "@myID": "2"
                    }
                }
            }
        }
    }
}


def flatten(d, parent_key='', sep='_'):
    """Return flattened dict as list"""
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
        if isinstance(v, _collections_abc.MutableMapping):
            items.extend(flatten(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)


def add_id(path, id_value):
    """Update list of @myID paths"""
    id_list.append([path, id_value])


def update_json(path, value):
    """Update the json_data with the default value"""
    l_bracket_q = "['"
    r_bracket_q = "']"
    at_default = "['@default']"
    result = ''
    cmd = "json_tree"
    for key in path:
        cmd += l_bracket_q + key + r_bracket_q
    cmd += at_default
    cmd += ' = value'
    exec(cmd)


flat_list = flatten(json_tree)
id_list = []

#  Loop the flattened list filtering for ID's, excluding the ones in count
for k, v in [x for x in flat_list.items() if '@myID' in x[0] and 'count' not in x[0]]:
    add_id(k, v)

route_list = []

# start building the route list from the filtered id list
for id_entry in id_list:
    route_list.append(
        [[x for x in id_entry[0].split('_') if x != '@myID'],
         [y for y in id_entry[1].split('.')]])

route_master = []

#  generate the route master to include the path and the full route (as list not 1.2.3)
for id_entry in id_list:
    s1 = id_entry[0].split('_')
    tmp_route = []
    tmp_list = []
    for i in range(len(s1)):
        if s1[i] == '@myID':
            break
        tmp_list.append(s1[i])
        for rte in route_list:
            if tmp_list == rte[0]:
                tmp_route.append(rte[1])
    route_item = list(itertools.chain(*tmp_route))
    tmp_list.append(route_item)
    route_master.append(tmp_list)

#  break out the default value from the route of the main driver file
l2 = list(zip(['.'.join(x.split('.')[:-1]) for x in l1], [x.split('.')[-1] for x in l1]))

#  loop the routes to process and update when found
for route, default in l2:
    for check_it in route_master:
        if route.split('.') == check_it[-1]:
            update_json(check_it[:-1], default)

#  print results
pprint(json_tree)

结果:

代码语言:javascript
复制
{'Gardens': {'Seaside': {'@loc': 'porch',
                         '@myID': '1.2.3',
                         'BackYard': {'@loc': 'backyard',
                                      '@myID': '75',
                                      'InfoList': {'count': {'@default': '0',
                                                             '@myID': '2'},
                                                   'status': {'@default': '2',
                                                              '@myID': '32'}},
                                      'Tid': '2'},
                         'InfoList': {'count': {'@default': '0', '@myID': '1'},
                                      'status': {'@default': '4',
                                                 '@myID': '26'}},
                         'Tid': '1'}}}

路由主控:

代码语言:javascript
复制
['Gardens', 'Seaside', ['1', '2', '3']]
['Gardens', 'Seaside', 'InfoList', 'status', ['1', '2', '3', '26']]
['Gardens', 'Seaside', 'BackYard', ['1', '2', '3', '75']]
['Gardens', 'Seaside', 'BackYard', 'InfoList', 'status', ['1', '2', '3', '75', '32']]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57326306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档