Pregunta

I have a callback function which is executed when I return from making a YQL statement:

function cbfunc(json)
{ 
   if (json.query.count)
   { 
      var data = json.query.results.json;
      $("#output").append(data.raw[0]["176932931"].name);
   }

This is the JSON object that I'm working with: http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/51694/members

I'm getting the result of the json and assigning it to the data variable. However, when I try to access the data variable (data.raw[0]["176932931"].name), it's giving me the following error: data.raw[0] is undefined.

This is what is returned when I console.log(json): http://i.imgur.com/mvYMYYI.png

This is what is returned when I console.log(data); http://i.imgur.com/fxBqvSJ.png

HOWEVER, when I assign the JSON string straight to the data variable, it works correctly, and I'm able to access properties of the object with no errors:

var data = {"raw":[{"176932931":{"name":"Shipdawg","blazeId":176932931,"clubStatus":0,"onlineStatus":0,"nucleusId":2266699357,"personaName":"Shipdawg"},"182141183":{"name":"Beks8","blazeId":182141183,"clubStatus":0,"onlineStatus":0,"nucleusId":2272736228,"personaName":"Beks8"},"219929617":{"name":"ChelseaFC_26","blazeId":219929617,"clubStatus":0,"onlineStatus":0,"nucleusId":2304510098,"personaName":"ChelseaFC_26"},"457588267":{"name":"Lazy__Rich","blazeId":457588267,"clubStatus":0,"onlineStatus":0,"nucleusId":2495578386,"personaName":"Lazy__Rich"},"517570695":{"name":"x0__andrew__0x","blazeId":517570695,"clubStatus":0,"onlineStatus":1,"nucleusId":2549150176,"personaName":"x0__andrew__0x"},"912396727":{"name":"mizz00-","blazeId":912396727,"clubStatus":0,"onlineStatus":1,"nucleusId":1000118566560,"personaName":"mizz00-"},"915144354":{"name":"MisterKanii","blazeId":915144354,"clubStatus":2,"onlineStatus":0,"nucleusId":2281969661,"personaName":"MisterKanii"}}]};

With this change, I get a slightly different object back when I console.log(data): http://i.imgur.com/VNCcwmx.png

But I can't use that as a solution since the data variable will be different depending on my YQL statement.

So the question is, how am I meant to access the properties of the first object? This one: http://i.imgur.com/fxBqvSJ.png

I've looked all over SO and Google and I just can't seem to find a solution.

Thanks.

¿Fue útil?

Solución

The JSON object you are getting back from that first link appears to be different from what displays from your console.log statements.

Specifically, in the example from EASports, the raw property is an array of length one containing a JSON object, yet what you seem to be dealing with is simply the JSON object itself.

To get you existing code working, you could do something like:

$("#output").append(data.raw["_12396727"].name);

In a more general sense, the use of Object.keys that @Neversay is proposing would allow you to iterate over the results contained in data.raw. So to access the underlying JSON object literal, you could use something like:

var keys = Object.keys(data.raw);

for(var i = 0; i < keys.length; i++) {
    var obj = data.raw[keys[i]];
    console.log(obj.name);
}

Otros consejos

Try Object.keys:

var key0 = Object.keys(data.raw)[0];
var name = data.raw[key0].name; ......

Interest, the result from EAsport is a list contains one object. But after being processed by YQL, the raw becomes a object, not an arrow anymore......

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top