JSON - searching through keys with variable names (unknown)

后端 未结 3 1310
感动是毒
感动是毒 2020-12-19 10:31

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 10:50

    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
    

提交回复
热议问题