views:

33

answers:

1

Hi all,

I am working on something using JSON and I can't seem to figure out how to do this. I'm receiving a JSON object that looks like this: {"List" : ["ob1":"c1","ob2":"c2"...]}

I am getting this from a .js file. How do I use the object?

So far I've only worked with files that have objects like this: Object = {"List" : ["ob1":"c1","ob2":"c2"...]}

And that's simple to use since all I have to do is import the .js file and then call Object.List [etc].

I'm a little bit of a noob so bear with me on this :( ..

Thanks in advance!

+2  A: 

If you mean that you are calling out to a service and it is returning the json back to you like that then you need to do something to get it into an object. Pretty much all of the popular javascript frameworks have a method to wrap this for you, in MooTools you would use Request.JSON instead of a normal request, and then it will parse the response and give you the basic object. If you really want to know the nitty gritty of parsing it out, then the source for MooTools or any of the other javascript frameworks out there would be a great source.

Boushley
Right. The underlying browser features are XMLHttpRequest and the `JSON` object or else `eval`; but you shouldn't write the code to do this yourself. If you're using jQuery, for example, you can just write: `$.getJSON(url, function (data) { alert("I got the data! " + data); });` http://api.jquery.com/jQuery.getJSON/ And if you're not already using a JS library... you should start!
Jason Orendorff
Okay, thanks guys! I'll look into these.
MapWeb