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

时间:2021-09-27 07:08:20

I want to search a JSON object to check if it contains string values stored in an array. and figure out the parent elements.

我想搜索JSON对象以检查它是否包含存储在数组中的字符串值。并找出父元素。

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);

      }
    }
  }

3 个解决方案

#1


0  

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));
});

#2


1  

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中的对象

#3


1  

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. 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. 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.

搜索功能将在json对象内搜索完全相等的值。例如,您可以使用它来搜索数组的每个元素,只需调整代码即可。堆栈是必要的,因为我们需要跟踪父母。我修改了你的json以插入更多关卡。结果的值是具有两个属性的对象。我认为通过这个你可以做你需要的。当然,您可以修改我的代码,以便在搜索中使用正则表达式的字符串。它会更强大。

#1


0  

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));
});

#2


1  

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中的对象

#3


1  

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. 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. 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.

搜索功能将在json对象内搜索完全相等的值。例如,您可以使用它来搜索数组的每个元素,只需调整代码即可。堆栈是必要的,因为我们需要跟踪父母。我修改了你的json以插入更多关卡。结果的值是具有两个属性的对象。我认为通过这个你可以做你需要的。当然,您可以修改我的代码,以便在搜索中使用正则表达式的字符串。它会更强大。