سؤال

I am trying to access an image in a JSON response however the field I need to access is an id value that is unique or rather is random. We are fetching this data from a server so we cannot hard code the id's.

The JSON is as follows:

 { "error" : { "occured" : "false" },
"errors" : [  ],
"executiontime" : 2500,
"metadata" : {  },
"value" : [ { "activity_duration" : "1 hour, ½ day & full day packages",
    "adult_rate_high_period_high_price" : 275,
    "adult_rate_high_period_low_price" : 49,
    "adult_rate_low_period_high_price" : "",
    "adult_rate_low_period_low_price" : "",
    "amenities" : [  ],
    "assets" : { "logo" : { "436209" : { "asset_type" : "image",
                "caption" : "",
                "credit" : "",
                "description" : "",
                "exists" : "true",
                "height" : 82,
                "label" : "Copy of Monarch logo",
                "latitude" : 0,
                "longitude" : 0,
                "market" : "$",
                "o_id" : 3221685,
                "type_o_id" : 2543991,
                "unique_id" : 436209,
                "url" : "http://c0481729.cdn2.cloudfiles.rackspacecloud.com/p-DD951E3E-C7AF-F22C-77E98D299833B38F-2544001.jpg",
                "width" : 220
              } },

We are trying to display the business logo for each amenity. To do this I need to access the url field in the above JSON. How do I access the url field under assest.

هل كانت مفيدة؟

المحلول

The Problem is to get the id of the Logo 436209.

var theid;
var l = obj.value[0].assets.logo
for (var p in l) {
  if (l[p].hasOwnProperty('unique_id')) {
     theid = l[p].unique_id;
     break;
  }
}

This is untestet. The idee is to use the in-operator to iterate over the properties of the logo-object and get the propterty that has the unique_id.

نصائح أخرى

Correction:

obj.value[0].assets.logo["436209"].url = 'foo';
// or
var foo = obj.value[0].assets.logo["436209"].url;

This assumes that your object is well formed and continues with more parts of obj.value[0].

Specifically, if your object were completed, perhaps, like this:

var obj = {
    "error": { "occured": "false" },
    "errors": [],
    "executiontime": 2500,
    "metadata": {},
    "value": [{
        "activity_duration": "1 hour, ½ day & full day packages",
        "adult_rate_high_period_high_price": 275,
        "adult_rate_high_period_low_price": 49,
        "adult_rate_low_period_high_price": "",
        "adult_rate_low_period_low_price": "",
        "amenities": [],
        "assets": {
            "logo": {
                "436209": {
                    "asset_type": "image",
                    "caption": "",
                    "credit": "",
                    "description": "",
                    "exists": "true",
                    "height": 82,
                    "label": "Copy of Monarch logo",
                    "latitude": 0,
                    "longitude": 0,
                    "market": "$",
                    "o_id": 3221685,
                    "type_o_id": 2543991,
                    "unique_id": 436209,
                    "url": "http://c0481729.cdn2.cloudfiles.rackspacecloud.com/p-DD951E3E-C7AF-F22C-77E98D299833B38F-2544001.jpg",
                    "width": 220
                }
            }
        }
    }]
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top