I changed the tree of my JSON-P output, and i cannot access to my object DOM anymore :
Here's my output :
jsonp1271634374310(
{"Inter-Medias":
{"name":"Inter-Medias","idGSP":"14","average":"80","services":"8.86"}
});
And here's my jQuery script :
success: function(data, textStatus, XMLHttpRequest){
widget = data.name;
widget += data.average ;
....
- I know one level is missing, 开发者_开发百科but if I try to do :
data.Inter-Medias.name
ordata.name.name
it's still not working. Any idea please ?
Thank you.
Your question is a bit confusing but I think what your issue is the JSON key. Try doing this:
data["Inter-Medias"].name
Now if you are expecting multiple objects you want the root element in your JSON to be an array.
[
{
"name" : "A",
"average" : 1
},
{
"name" : "B",
"average" : 2
}
]
In code you can access it like this.
var obj = null, length = data.length;
for (var i = 0; i < length; i++) {
obj = data[i];
if (obj.name === "A") {
runningAverage += obj.average;
}
}
精彩评论