mardi 22 avril 2014

JavaScript - impossible d'accéder à un champ dans JSON - Stack Overflow


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.


We are now able to grab the ID thanks to the accepted answer however now we are having trouble grabbing the url from the url field in assets. Below is our code:


Quick question. We can get the ID. but how do we get the URL out. We getting undefined errors. We have tried the following:$.getJSON(URL, function (data) { $.each(data.value, function (i, item) { //creates an array of type DataSet_Class


    var theid;
var l = item.assets.logo
for (var p in l) {
if (l[p].hasOwnProperty('unique_id')) {
theid = l[p].unique_id;
console.log(item.assets[theid].url);
break;
}
}



dataSetArray.push(new DataSet_Class(item.nameoflisting, item.listing_summary, item.website_link, item.latitude, item.longitude, item.listing_types));
});
ManageDataSet(dataSetArray); //markers are created
$("#Loading").hide(); //hides Loading animation
$("#NavigationDiv").show(); //Displays navigation div.

});




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
}
}
}
}]
};


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.


We are now able to grab the ID thanks to the accepted answer however now we are having trouble grabbing the url from the url field in assets. Below is our code:


Quick question. We can get the ID. but how do we get the URL out. We getting undefined errors. We have tried the following:$.getJSON(URL, function (data) { $.each(data.value, function (i, item) { //creates an array of type DataSet_Class


    var theid;
var l = item.assets.logo
for (var p in l) {
if (l[p].hasOwnProperty('unique_id')) {
theid = l[p].unique_id;
console.log(item.assets[theid].url);
break;
}
}



dataSetArray.push(new DataSet_Class(item.nameoflisting, item.listing_summary, item.website_link, item.latitude, item.longitude, item.listing_types));
});
ManageDataSet(dataSetArray); //markers are created
$("#Loading").hide(); //hides Loading animation
$("#NavigationDiv").show(); //Displays navigation div.

});



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
}
}
}
}]
};

0 commentaires:

Enregistrer un commentaire