JSON - 使用变量名称搜索键(未知) [英] JSON - searching through keys with variable names (unknown)

查看:91
本文介绍了JSON - 使用变量名称搜索键(未知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处共有JSON noob。我试图循环一些JSON从对象内的数组中取出第一个图像,经过4个小时后,我决定我可能需要一些帮助。

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.

我能够从我知道密钥的对象中提取我需要的每个值,但是我有一些数据具有非一致的密钥名称,我需要基本上通过寻找部分匹配来迭代然后拉出这些结果中的第一个。

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.

未知元素的Json结构的结构如下:

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

"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": [
      ""
        ]
}

我所追求的是这种情况下的content_2_image,但在另一种情况下输入它可能是content_20_image我所知道的(有很多数据被拉出)。

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

任何关于循环通过这些未知键寻找部分的最佳方法的想法非常感谢在钥匙或其他东西上匹配'_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.

谢谢!

推荐答案

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

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)循环通过json对象的键(json中的var键)

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

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

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

3)检查密钥是否与正则表达式匹配 / content_ [0-9] + _ image /

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

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

3a) Basically, test if it matches content_ANY NUMBERS_image where ANY NUMBERS is equal to at least one digit or more

4)请使用该数据 console.log(json [key ])

希望这会有所帮助!

这篇关于JSON - 使用变量名称搜索键(未知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆