views:

48

answers:

1

Hello,

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 ;
....
  1. I know one level is missing, but if I try to do : data.Inter-Medias.name or data.name.name it's still not working. Any idea please ?

Thank you.

+2  A: 

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;
    }    
}
ChaosPandion
sorry if my questions were confusing, you were right but i gave the case where i know that the result would be "Inter-Medias" but what i put for the general cases where it would work for everything please ?
Tristan