jQuery JSON - 搜索具有变量名称的键(未知)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18910976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:45:25  来源:igfitidea点击:

JSON - searching through keys with variable names (unknown)

javascriptjqueryjsonjsonp

提问by Steve de Niese

Total JSON noob here. I'm trying to cycle through some JSON to pull out the first image from an array inside the object, and after 4 hours of having at it, I've decided I probably need some help.

这里的总 JSON 菜鸟。我正在尝试通过一些 JSON 循环从对象内部的数组中提取第一个图像,在使用了 4 个小时之后,我决定我可能需要一些帮助。

I'm able to pull every value I need out of the object where I know the key, but I have some data that has non consistent key names that I need to basically iterate through looking for a partial match and then pulling the first on of these results.

我能够从我知道键的对象中提取我需要的每个值,但是我有一些具有不一致键名的数据,我需要基本上遍历查找部分匹配,然后提取第一个这些结果。

The Json structure of the unknown element is structured like this:

未知元素的Json结构是这样的:

"custom_fields": {
    "content_0_subheading": [
      "Title text"
    ],
    "content_1_text": [
      "Some text"
    ],
    "content_2_image": [
      [
        "http://staging.livelivelyblog.assemblo.com/wp-content/uploads/2013/09/wellbeing-260x130.jpg",
        260,
        130,
        true
      ]
    ],
    "content_2_caption": [
      ""
        ]
}

What I'm after is the content_2_image in this case, but in another entry it could be content_20_image for all I know (there's a lot of data being pulled).

在这种情况下,我所追求的是 content_2_image ,但在另一个条目中,据我所知,它可能是 content_20_image (有很多数据被提取)。

Any ideas of the best way to cycle through these unknown keys looking for a partial match on '_image' in the key or something, would be VERY appreciated.

任何有关循环浏览这些未知键以寻找键中“_image”部分匹配或其他内容的最佳方式的任何想法,都将不胜感激。

Thanks!

谢谢!

回答by switz

You can't just search through every field with a partial match, so you'll have to iterate through every field and then check the field names for the match. Try something like this:

您不能只搜索具有部分匹配的每个字段,因此您必须遍历每个字段,然后检查匹配的字段名称。尝试这样的事情:

var json = {
  "content_0_subheading": [
    "Title text"
  ],
  "content_1_text": [
    "Some text"
  ],
  "content_2_image": [
    [
      "http://staging.livelivelyblog.assemblo.com/wp-content/uploads/2013/09/wellbeing-260x130.jpg",
      260,
      130,
      true
    ]
  ],
  "content_2_caption": [
    ""
  ]
}

for (var key in json) {
    if (json.hasOwnProperty(key)) {
        if (/content_[0-9]+_image/.test(key)) {
            console.log('match!', json[key]); // do stuff here!
        }
    }
}

Basically, what we're doing is:

基本上,我们正在做的是:

1) Loop through keys of json object for (var key in json)

1) 循环遍历 json 对象的键 for (var key in json)

2) Ensure the json has the property, and we're not accessing keys we don't want if (json.hasOwnProperty(key))

2)确保json具有属性,并且我们没有访问我们不想要的密钥 if (json.hasOwnProperty(key))

3) Check if key matches the regular expression /content_[0-9]+_image/

3)检查key是否与正则表达式匹配 /content_[0-9]+_image/

3a) Basically, test if it matches content_ANY NUMBERS_imagewhere ANY NUMBERSis equal to at least one digit or more

3a) 基本上,测试它是否匹配content_ANY NUMBERS_imagewhereANY NUMBERS等于至少一位或更多

4) Use that data however you please console.log(json[key])

4)随意使用这些数据 console.log(json[key])

Hope this helps!

希望这可以帮助!

回答by MrP

You could use for ... in

你可以用 for ... in

for (key in object) {
    // check match & do stuff
}

回答by u283863

var json = JSON.parse(YOUR_JSON_STRING).custom_fields, //Fetch your JSON
    image;                                             //Pre-declare image
for(key in json){                               //Search each key in your object
    if(key.indexOf("image") != -1){             //If the index contains "image"
        image = json[key];                //Then image is set to your image array
        break;                                  //Exit the loop
    }
}
/*
image[0]  //the URL
image[1]  //the width
image[2]  //the height
image[3]  //your boolean