JSON在嵌套字典中获取密钥路径 [英] JSON get key path in nested dictionary

查看:102
本文介绍了JSON在嵌套字典中获取密钥路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ {
$ {
花{
花:{
:玫瑰,
白花:茉莉花,
黄花:万寿菊
}
},
水果 {
黄色水果:芒果,
绿色水果:番石榴,
白花:groovy
},
树木:{
label:{
黄色水果:南瓜,
白花:Bogan
}
}
}'

这是我的json字符串,它不断变化,所以键位置在字典每次都不一样,我需要
搜索一个键并打印它对应的值,因为每次我写了一个递归函数(见下文)来搜索json字符串,以搜索
在新的json字符串和打印t他值得然而现在情况是我们使用diff值多次使用相同的密钥,
i如何获得密钥的完整路径,因此更容易理解它的关键值,例如,结果应该是这样的:

  app.Garden.Flowers.white花=茉莉花
app.Fruits.White花= groovy
app.Trees.label.White Flower = Bogan

我的代码到目前为止:

  import json 
with open('data.json')as data_file:
j = json。 load(data_file)

#j = json.loads(a)


def find(element,JSON):
如果JSON中的元素:
打印JSON [元素] .encode('utf-8')
为JSON中的键:
如果isinstance(JSON [key],dict):
find(element, JSON [key])



find(要搜索的元素,j)


解决方案

您可以添加一个跟踪当前的字符串参数JSON路径。如下所示:

  def find(element,JSON,path,all_paths):
if element in JSON:
path = path + element +'='+ JSON [element] .encode('utf-8')
打印路径
all_paths.append(path)
JSON:
if isinstance(JSON [key],dict):
find(element,JSON [key],path + key +'。',all_paths)

您可以这样称呼:

  all_paths = [] 
find(element_to_search,j,'',all_paths)


json = '{
    "app": {
        "Garden": {
            "Flowers": {
                "Red flower": "Rose",
                "White Flower": "Jasmine",
                "Yellow Flower": "Marigold"
            }
        },
        "Fruits": {
            "Yellow fruit": "Mango",
            "Green fruit": "Guava",
            "White Flower": "groovy"
        },
        "Trees": {
            "label": {
                "Yellow fruit": "Pumpkin",
                "White Flower": "Bogan"
            }
        }
    }'

Here is my json string, which keeps on changing frquently so the keys position within the dictionary is not same everytime, i need to search for a key and print it corresponding value, Since the json string changes everytime I have written an recursive function(See below) to search for key in the new json string and print the value. However now the situation is we have same key multiple times with diff values, how can i get the complete path of the key so it would be easier to understand which key value it is, for example the result should be like this:

app.Garden.Flowers.white Flower = Jasmine
app.Fruits.White Flower = groovy
app.Trees.label.White Flower = Bogan

My code so far:

import json
with open('data.json') as data_file:    
  j = json.load(data_file)

# j=json.loads(a)


def find(element, JSON):    
  if element in JSON:
    print JSON[element].encode('utf-8')
  for key in JSON:
    if isinstance(JSON[key], dict):
      find(element, JSON[key])



find(element to search,j)

解决方案

You could add a string parameter that keeps track of the current JSON path. Something like the following could work:

def find(element, JSON, path, all_paths):    
  if element in JSON:
    path = path + element + ' = ' + JSON[element].encode('utf-8')
    print path
    all_paths.append(path)
  for key in JSON:
    if isinstance(JSON[key], dict):
      find(element, JSON[key],path + key + '.',all_paths)

You would call it like this:

all_paths = []
find(element_to_search,j,'',all_paths)

这篇关于JSON在嵌套字典中获取密钥路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆