> From: jeff w
> 
> I am new to jQuery, and have started to play with JSON,but I 
> need some info about how I refer to the JSON Object once it 
> is  returned from the server. I know I can loop through the 
> contents of the object, and I can use json.count, but I am 
> really unsure about the correct syntax to target the data 
> that I need. Can anyone provide a link to a tutorial or some 
> other help?
> 
> Here is the JSON object that I need to return from the server:
> 
> {"models": ["MDX SUV", "RDX SUV", "RL Sedan", "TL Sedan", "TSX Sedan"]}
> 
> Thanks for your help.
> 
> ************
> 
> since writing this, I have made a guess at what might work. I 
> confirmed that the data is returning as stated above (using 
> Firebug), but when I echo json.count, i get 'undefined'. does 
> that make sense?

I don't see a count property in your JSON data, and you didn't mention
eval'ing the JSON string (it's just a string until you do something with
it).

Assuming that you have received a string from your server in a variable
"json" containing the above JSON text:

   json = eval( '(' + json + ')' );
   var models = json.models;
   for( var i = 0, n = models.length;  i < n;  ++i ) {
      var model = models[i];
      console.debug( model );
   }

You can even use $.each if you like:

   json = eval( '(' + json + ')' );
   $.each( json.models, function( i, model ) {
      console.debug( model );
   });

-Mike

Reply via email to