简体   繁体   English

Javascript:在JSON中查找键及其值

[英]Javascript: Find key and its value in JSON

I have a JSON object that is returned in different ways, but always has key . 我有一个以不同方式返回的JSON对象,但始终具有key How can I get it? 我怎么才能得到它?

Eg 例如

"Records": {
    "key": "112"
}

Or 要么

"Records": { 
    "test": {
        "key": "512"
    }
}

Or even in array: 甚至是数组:

"Records": { 
    "test": {
        "test2": [
            {
                "key": "334"
            }
        ]
    }
}

Tried several options, but still can't figure out ( 尝试了几种选择,但仍然无法弄清楚(

I will not write the code for you but give you an idea may be it will help, First convert JSON object in to string using 我不会为您编写代码,但会给您一个想法,可能会有所帮助,首先使用以下方法将JSON对象转换为字符串

JSON.stringify(obj);

after that search for Key using indexOf() method. 之后,使用indexOf()方法搜索Key。 Extract previous '{' and Next '}' string and again cast in to JSON object. 提取上一个'{'和Next'}'字符串,然后再次强制转换为JSON对象。 using 运用

var obj =  JSON.parse(string);

Then 然后

 var value = obj.key

I think this migth be solution (asuming key is always string and you don't care about res of data) 我认为这种解决方案是解决方案(假设键始终是字符串,并且您不关心数据的分辨率)

 const data = [`"Records": { "test": { "test2": [ { "key": "334", "key": "3343" } ] } }`, `"Records": { "test": { "key": "512" } }`, `"Records": { "key": "112" }`] const getKeys = data => { const keys = [] const regex = /"key"\\s*:\\s*"(.*)"/g let temp while(temp = regex.exec(data)){ keys.push(temp[1]) } return keys } for(let json of data){ console.log(getKeys(json)) } 

How can i get it? 我怎么才能得到它?

Recursively! 递归! eg 例如

function getKey(rec) {
    if (rec.key) return rec.key;

    return getKey(rec[Object.keys(rec)[0]]);
}

https://jsfiddle.net/su42h2et/ https://jsfiddle.net/su42h2et/

You could use an iterative and recursive approach for getting the object with key in it. 您可以使用迭代和递归的方法来获取带有key的对象。

 function getKeyReference(object) { function f(o) { if (!o || typeof o !== 'object') { return; } if ('key' in o) { reference = o; return true; } Object.keys(o).some(function (k) { return f(o[k]); }); } var reference; f(object); return reference; } var o1 = { Records: { key: "112" } }, o2 = { Records: { test: { key: "512" } } }, o3 = { Records: { test: { test2: [{ key: "334" }] } } }; console.log(getKeyReference(o1)); console.log(getKeyReference(o2)); console.log(getKeyReference(o3)); 

You can try this 你可以试试这个

 const data = { "Records": { "key": "112" } }; const data2 = { "Records": { "test": { "key": "512" } } }; const data3 = { "Records": { "test": { "test2": [ { "key": "334" }, ] } } }; function searchKey(obj, key = 'key') { return Object.keys(obj).reduce((finalObj, objKey) => { if (objKey !== key) { return searchKey(obj[objKey]); } else { return finalObj = obj[objKey]; } }, []) } const result = searchKey(data); const result2 = searchKey(data2); const result3 = searchKey(data3); console.log(result); console.log(result2); console.log(result3); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM