最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the child key names of a JSON object - Stack Overflow

programmeradmin2浏览0评论

I need to get the xpath of json (ie) basically i need to get the keys of the jsons along with its xpath.

var json='"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML",
                    "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}

Object.keys(json)

The above code returns the key of the parent structure as below.

[ 'glossary' ]

I need all the keys along with the path.

I need to get the xpath of json (ie) basically i need to get the keys of the jsons along with its xpath.

var json='"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML",
                    "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}

Object.keys(json)

The above code returns the key of the parent structure as below.

[ 'glossary' ]

I need all the keys along with the path.

Share Improve this question edited Sep 20, 2023 at 20:22 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked May 30, 2013 at 10:51 Amanda GAmanda G 1,99111 gold badges33 silver badges44 bronze badges 2
  • There is no such thing as XPath for Json. There is an equivalent named JSONPath, did you mean that? Please clarify. – dirkk Commented May 30, 2013 at 11:10
  • Yes i meant that only – Amanda G Commented May 31, 2013 at 4:13
Add a ment  | 

2 Answers 2

Reset to default 3

Maybe this is what you're looking for:

var json={"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML",
                    "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}};

function getKeys(keys, obj, path) {
    for(key in obj) {
        var currpath = path+'/'+key;
        keys.push([key, currpath]);
        if(typeof(obj[key]) == 'object' && !(obj[key] instanceof Array))
            getKeys(keys, obj[key], currpath);
    }
}

var keys = [];
getKeys(keys, json, '');
for(var i=0; i<keys.length; i++)
    console.log(keys[i][0] + '=' + keys[i][1]);

the result is:

glossary=/glossary

title=/glossary/title

GlossDiv=/glossary/GlossDiv

title=/glossary/GlossDiv/title

GlossList=/glossary/GlossDiv/GlossList

GlossEntry=/glossary/GlossDiv/GlossList/GlossEntry

ID=/glossary/GlossDiv/GlossList/GlossEntry/ID

SortAs=/glossary/GlossDiv/GlossList/GlossEntry/SortAs

...

You can do that with following code:

public  void getKeys(Set<String> keys,JSONObject obj,String path) {
    Iterator<String> keys1 = obj.keys();
    while(keys1.hasNext()) {
        String key = keys1.next();
        String currpath=path+'/'+key;
        keys.add(currpath);
        if (obj.get(key) instanceof JSONObject) {
            getKeys(keys, obj.getJSONObject(key), currpath);
        }
        if (obj.get(key) instanceof JSONArray) {
            JSONArray arr=(JSONArray) obj.get(key);
            for(int i=0;i<arr.length();i++) {
                JSONObject nobj=(JSONObject) arr.get(i);
                getKeys(keys, nobj, currpath);
            }
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论