简体   繁体   English

如何在JSON对象中搜索特定的字符串值?

[英]How do I search a JSON object for a specific string value?

I want to search a JSON object to check if it contains string values stored in an array. 我想搜索一个JSON对象,以检查它是否包含存储在数组中的字符串值。 and figure out the parent elements. 并找出父元素。

var searchVal = ['name_1a','name_2y','name_3x'];

var json = {
"location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        },
    }

    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        },
    }
};

I want to pass the results into a function. 我想将结果传递给函数。 And deal with them separate. 并分开处理。

name_1a -> function(title2b, object)
name_2y -> function(title2y, object)
name_3x -> function(title1x, location) etc.

. This is what I have tried so far. 到目前为止,这是我尝试过的。 I can't seem to figure out how to gothrough the entire JSON object 我似乎无法弄清楚如何遍历整个 JSON对象

var searchVal = ['name_1a','name_2y','name_3x'];
  for (var i=0 ; i < searchVal.length ; i++){
    for (var k=0 ; k < ????.length ; k++)
    {
      if (json.???????? == searchVal[i]) {
        results.push(???????);
        console.log(results);

      }
    }
  }

with the code below you can find what you are looking for recursively: 使用下面的代码,您可以递归找到所需内容:

var json = {
    "location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        },
    },
    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        },
    }
};

var searchTest = function(varToSearch, jsonData) {

    for (var key in jsonData) {
        if(typeof(jsonData[key]) === 'object') {
            searchTest(varToSearch, jsonData[key]);
        } else {
            if(jsonData[key] == varToSearch) {
                console.log(jsonData[key]);
            }
        }
    }

}

searchTest('name_1a', json);

Reference: 参考:

get data from dynamic key value in json 从JSON中的动态键值获取数据

get value from json with dynamic key 使用动态键从json获取值

https://trinitytuts.com/tips/get-dynamic-keys-from-json-data/ https://trinitytuts.com/tips/get-dynamic-keys-from-json-data/

How do I enumerate the properties of a JavaScript object? 如何枚举JavaScript对象的属性?

Check if a value is an object in JavaScript 检查值是否是JavaScript中的对象

What about something like this: 像这样的事情呢:

 var json = { "location": { "title1x": { "1": "name_1x", "2": "name_2x", "3": "name_3x", }, "title2y": { "1": "name_1y", "2": "name_2y", "3": "name_3y", }, }, "object": { "title1a": { "1": "name_1z", "2": "name_2z", "3": "name_3z", }, "title2b": { "1": "name_1a", "2": "name_2a", "3": "name_3a", "foo": [{ "x": "aaa", "y": "bbb", "z": { "k": "name_3y" } }, { "x": "aaa", "y": "bbb", "z": { "k": "name_3y", "bar": [{ "op": "test", "fooAgain": { "x": "name_3y" } }] } }] }, } }; function search(what, where) { var results = []; var parentStack = []; var searchR = function(what, where) { if (typeof where == "object") { parentStack.push(where); for (key in where) { searchR(what, where[key]); }; parentStack.pop(); } else { // here comes your search if (what === where) { results.push({ parent: parentStack[parentStack.length - 1], value: where }); } } } searchR(what, where); return results; } search("name_3y", json).forEach(function(value, key) { var out = "parent: \\n"; for (key in value.parent) { out += " key: " + key + " - value: " + value.parent[key] + "\\n"; } out += "\\nvalue: " + value.value; alert(out); }); 

The search function will search for a value that is exactly equal inside a json object. 搜索功能将在json对象内部搜索一个完全相等的值。 You can use it to search for each element of an array for example, just adjust the code. 例如,您可以使用它来搜索数组的每个元素,只需调整代码即可。 A stack was necessary, since we need to keep track of the parents. 需要堆栈,因为我们需要跟踪父母。 I modified your json to insert more levels. 我修改了您的json以插入更多级别。 The values of the results are objects with two attributes. 结果的值是具有两个属性的对象。 I think that with this you can do what you need. 我认为,您可以根据自己的需要做。 You can, of course, modify my code to use regular expressions intead of strings in your search. 当然,您可以修改我的代码以在搜索中使用包含字符串的正则表达式。 It would be more powerfull. 它会更强大。

 var searchVal = ['name_1a','name_2y','name_3x']; var json = { "location": { "title1x": { "1": "name_1x", "2": "name_2x", "3": "name_3x", }, "title2y": { "1": "name_1y", "2": "name_2y", "3": "name_3y", } }, "object": { "title1a": { "1": "name_1z", "2": "name_2z", "3": "name_3z", }, "title2b": { "1": "name_1a", "2": "name_2a", "3": "name_3a", } } }; var getTitle=function(json,val){ for (var key in json) { var titles= json[key]; for (var tit in titles) { var names=titles[tit]; for (var name in names) { var string=names[name]; if(string===val) return tit; } } } } searchVal.forEach(function(valToSearch){ console.log(getTitle(json,valToSearch)); }); 

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

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