admin管理员组

文章数量:1205147

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.

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": [
      [
        ".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).

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.

Thanks!

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.

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

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

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.

Thanks!

Share Improve this question asked Sep 20, 2013 at 7:16 Steve de NieseSteve de Niese 9842 gold badges10 silver badges18 bronze badges 1
  • do you control the JSON structure or is this some API call response? – bruchowski Commented Sep 20, 2013 at 7:20
Add a comment  | 

3 Answers 3

Reset to default 11

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)

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

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

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

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

Hope this helps!

You could use for ... in

for (key in object) {
    // check match & do stuff
}
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

本文标签: javascriptJSONsearching through keys with variable names (unknown)Stack Overflow