嵌套复杂JSON中的搜索键 [英] Search key in nested complex JSON
问题描述
我必须通过JavaScript或jQuery搜索嵌套JSON中的密钥。在我的JSON对象中,所有键都是唯一的。我自己尝试了一些解决方案,但它们没有用。
这是我的代码:
I have to search for a key in nested JSON by JavaScript or by jQuery. In my JSON object all the keys are unique. I tried some solutions myself but they did not work. Here is my code:
json = {
"app": [{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{"type a":"Pumpkin", "type b": "Banana",..}],
"White Flower 2": ["Bogan 1", "Bogan 2", ...]
}
}],...
}
如何搜索对于给定对象中的特定键?
How can I search for a specific key in given object?
如果我传递 lookup(json,type a)
它应该返回Pumpkin
,或者如果我搜索白花2
它应该返回 [Bogan 1,Bogan 2,...]
If I pass lookup(json, "type a")
it should return "Pumpkin"
, OR If I search for "White Flower 2"
it should return ["Bogan 1", "Bogan 2", ...]
这是我的尝试,这是行不通的:
Here is my try, which is not working:
function lookup(obj, k){
for (key in obj){
value = obj[key];
if (k == key) return [k, value];
if (type(value) == "Object"){
var y = lookup(value, k);
if (y && y[0]== k)return y;
}
if(type(value) == "Array"){
for (i in value)
{
var x = lookup(value[i], k);
if (x && x[0]== k)return x;
}
}
console.log(key, value);
return null;
}
}
要查找对象的类型,我是使用此代码:
To find the type of the object, I'm using this code:
function type(object){
var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = {}.constructor;
if (object === null) {
return "null";
}
else if (object === undefined) {
return "undefined";
}
else if (object.constructor === stringConstructor) {
return "String";
}
else if (object.constructor === arrayConstructor) {
return "Array";
}
else if (object.constructor === objectConstructor) {
return "Object";
}
else {
return "null";
}
}
推荐答案
你'比你想象的更近 - 移动返回null;
用于中的(键入obj)
是主要的东西;否则,只要对象中的第一个键不匹配,您就会放弃。只有在搜索完所有键后才放弃。
You're closer than you think - moving return null;
out of for (key in obj)
is the main thing; otherwise, you're giving up as soon as the first key in the object doesn't match. Only give up after searching all the keys.
function lookup(obj, k) {
for (key in obj) {
value = obj[key];
if (k == key) return [k, value];
if (type(value) == "Object") {
var y = lookup(value, k);
if (y && y[0] == k) return y;
}
if (type(value) == "Array") {
// for..in doesn't work the way you want on arrays in some browsers
//
for (var i = 0; i < value.length; ++i) {
var x = lookup(value[i], k);
if (x && x[0] == k) return x;
}
}
}
return null;
}
var json = {
"app": [{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{
"type a": "Pumpkin",
"type b": "Banana"
}],
"White Flower 2": ["Bogan 1", "Bogan 2"]
}
}
}]
}
function type(object) {
var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = {}.constructor;
if (object === null) {
return "null";
} else if (object === undefined) {
return "undefined";
} else if (object.constructor === stringConstructor) {
return "String";
} else if (object.constructor === arrayConstructor) {
return "Array";
} else if (object.constructor === objectConstructor) {
return "Object";
} else {
return "null";
}
}
console.log(lookup(json, 'type a'));
console.log( lookup(json, 'White Flower 2') );
ps没有JSON对象这样的东西。如果它不是字符串,那么它不是JSON。您正在搜索JavaScript对象。
这篇关于嵌套复杂JSON中的搜索键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!