开发者

Problem with accessing in DOM with jQuery

开发者 https://www.devze.com 2022-12-27 13:36 出处:网络
I changed the tree of my JSON-P output, and i cannot access to my object DOM anymore : Here\'s my output :

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号