[jQuery] Re: Basic JSON help
I'm not quite sure which part you're asking about. To get and interact with your example data, you could do something like: $.getJSON('/url/to/json', function(data) { for(var i = 0; i data.models.length; i++) { $('#output').append('div' + data.models[i] + '/div'); // or whatever you want to do with it. } }); Does that help? There's lots of info on JSON out there. Google can help you with that. --Erik On 8/16/07, jeff w [EMAIL PROTECTED] wrote: Hello, 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.
[jQuery] Re: Basic JSON help
Right, 'count' doesn't have any special meaning that I know of. What are you expecting it to be? Arrays have a special property called length, which you could access like json.models.length, but Objects (like your json variable), don't necessarily have this special property. --Erik On 8/16/07, jeff w [EMAIL PROTECTED] wrote: Hello, 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?
[jQuery] Re: Basic JSON help
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
[jQuery] Re: Basic JSON help
I assumed he was using $.getJSON or something similar that takes care of the eval'ing for you. --Erik On 8/16/07, Michael Geary [EMAIL PROTECTED] wrote: 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
[jQuery] Re: Basic JSON help
Oh, you're right, Erik, I wasn't paying close enough attention to the related thread. Jeff, skip the eval in the code I listed if it's coming from $.getJSON. :-) -Mike _ From: Erik Beeson I assumed he was using $.getJSON or something similar that takes care of the eval'ing for you. --Erik On 8/16/07, Michael Geary [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: 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
[jQuery] Re: Basic JSON help
Thats it! I was referring to the length as 'count'. Thats why my for loop wasn't executing, and thats why I couldn't echo my data in the browser. Thanks! So, should I treat my JSON object like a Javascript multi-dimensional array? On Aug 16, 10:20 am, Erik Beeson [EMAIL PROTECTED] wrote: Right, 'count' doesn't have any special meaning that I know of. What are you expecting it to be? Arrays have a special property called length, which you could access like json.models.length, but Objects (like your json variable), don't necessarily have this special property. --Erik On 8/16/07, jeff w [EMAIL PROTECTED] wrote: Hello, 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?
[jQuery] Re: Basic JSON help
Yes, I am using $.getJSON. eval() is a javascript function right? so if I want more info on that, I should look at a javascript reference?? what is console.debug()? I've seen that in a bunch of posts. I am guessing its a way to output results. Is it a cleaner way of using something like alert(spit out results);? Thanks for your patience if my questions are elementary, I'm just trying to build some sort of foundation of basic concepts. On Aug 16, 10:51 am, Erik Beeson [EMAIL PROTECTED] wrote: I assumed he was using $.getJSON or something similar that takes care of the eval'ing for you. --Erik On 8/16/07, Michael Geary [EMAIL PROTECTED] wrote: 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
[jQuery] Re: Basic JSON help
Like Mike said, you don't need eval() when using $.getJSON(). That console.* stuff is part of a FireFox extension called FireBug that's fairly standard for doing development. --Erik On 8/16/07, jeff w [EMAIL PROTECTED] wrote: Yes, I am using $.getJSON. eval() is a javascript function right? so if I want more info on that, I should look at a javascript reference?? what is console.debug()? I've seen that in a bunch of posts. I am guessing its a way to output results. Is it a cleaner way of using something like alert(spit out results);? Thanks for your patience if my questions are elementary, I'm just trying to build some sort of foundation of basic concepts. On Aug 16, 10:51 am, Erik Beeson [EMAIL PROTECTED] wrote: I assumed he was using $.getJSON or something similar that takes care of the eval'ing for you. --Erik On 8/16/07, Michael Geary [EMAIL PROTECTED] wrote: 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
[jQuery] Re: Basic JSON help
So, should I treat my JSON object like a Javascript multi-dimensional array? Seeing as how JavaScript doesn't *have* multi-dimensional arrays, I probably wouldn't put it exactly that way. :-) But you definitely have the right idea. JSON is simply a text representation of JavaScript objects, arrays, or other JavaScript types. In other words, JSON *is* JavaScript code. So, once you eval it and have a reference to the eval'ed data, treat it exactly as you would any other JavaScript object or array - whatever the data type is. Pure JavaScript: var foo = { a:1, b:2 }; alert( foo.a ); // 1 Or the same thing with quotes: var foo = { a:1, b:2 }; alert( foo.a ); // 1 JSON+JavaScript var jsonText = '{ a:1, b:2 }'; // The '(' and ')' are required for syntactic reasons var jsonObject = eval( '(' + jsonText + ')' ); alert( jsonObject.a ); // 1 Or alternatively - does the same thing: var jsonText = '{ a:1, b:2 }'; eval( 'var jsonObject = ' + jsonText ); alert( jsonObject.a ); // 1 In the case of $.getJSON, the eval has already been done for you, and the resulting jsonObject is passed to your callback function. -Mike
[jQuery] Re: Basic JSON help
Jeff, Firebug and Web Developer extensions for Firefox will become very useful for you. There is also a jQuery extension for Firebug debugging: http://jquery.glyphix.com/ Good instructions on that page. A JSON (or other Javascript) Object != a multidimensional array. Javascript objects can be accessed like an array, but are defined differently. One of the important differences is that Objects do not have a .length property by default. In Mike's example, you can access the length property of 'models' because models IS an array, defined by brackets ( [ ] ). This is more complex than I can properly describe, there is an excellent writeup at Quirksmode: http://www.quirksmode.org/js/associative.html The more you understand about Javascript, the easier jQuery will become for you. Though jQuery makes many things simpler and faster, it is a deep understanding of Javascript that will really open the power of jQuery to you. I recommend keeping Javascript reference guides open in browser tabs, such as http://www.w3schools.com/ W3C schools for the basics and http://www.quirksmode.org/ Quirksmode for the weird cases Google is also pretty good for searching code cases. Best of luck in your quest! Hope this is all helpful to you. Charles doublerebel.com On Aug 16, 9:51 am, Erik Beeson [EMAIL PROTECTED] wrote: Like Mike said, you don't need eval() when using $.getJSON(). That console.* stuff is part of a FireFox extension called FireBug that's fairly standard for doing development. --Erik On 8/16/07, jeff w [EMAIL PROTECTED] wrote: Yes, I am using $.getJSON. eval() is a javascript function right? so if I want more info on that, I should look at a javascript reference?? what is console.debug()? I've seen that in a bunch of posts. I am guessing its a way to output results. Is it a cleaner way of using something like alert(spit out results);? Thanks for your patience if my questions are elementary, I'm just trying to build some sort of foundation of basic concepts. On Aug 16, 10:51 am, Erik Beeson [EMAIL PROTECTED] wrote: I assumed he was using $.getJSON or something similar that takes care of the eval'ing for you. --Erik On 8/16/07, Michael Geary [EMAIL PROTECTED] wrote: 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