Question

For some reason I just can't seem to be able to display properties from this JSON string: http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/51694/members

I've sat here for the last 2-3 hours trying out different ways to select single properties such as the name of the first person in the array. A couple selectors I've tried:

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

I always get the same error. "data.raw[0] is undefined". The JSON string is valid, I'm able to output the whole string to my page using:

document.getElementById('output').innerHTML=data.toSource();

Parsing it into a JSON object gives me another error because it already is a JSON object. By using console.log(data) I'm able to view the JSON object properly in Firebug.

data is the name of the Javascript JSON object variable that is being returned from my YQL statement.

Please, if anyone could provide some examples as to how I should go about accessing the properties of the above JSON string, that would be great.

UPDATE:

Here's the callback function from my YQL statement:

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

Solution

You need to use bracket notation, as identifiers starting with digits are invalid

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

OTHER TIPS

as "176932931" is an integer key so you have to access like json["176932931"].

For example

data.raw[0]["176932931"].name

see fiddle here

.count isn't a property of a json object. Try this:

var something = {"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"}}]}


function cbfunc(json)
{ 
   if (json.raw.length)
   { 
      $("#output").append(json.raw["0"]["176932931"].name);
   }
}

cbfunc(something);

Tell me if this works for you:

function cbfunc(json)
{

    $each(json, function(key, object){
         console.log(key, object);
    });
    var raw = query.results.json.raw;
    console.log(raw );


    // uncomment it if you want some extra check.
    if (/*typeof data.raw !=='undefined' && */data.raw.length > 0)
    { 
        //console.log(data.raw[0]["176932931"].name);
        //$("#output").append(data.raw[0]["176932931"].name);
    }
}

If this works for you there's no need to reference the object to data, simply use the object its self.

JS fiddle: http://jsfiddle.net/q8xL3/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top