I know in concept, or reality even they are essentially one in the same. However I know working with JSON objects (as of the time of writing this at least). That Altering the data within a given set of objects isn’t the easiest thing. Of course I could be wrong, there could be an easy means of doing it all JSON. Which leads me to my actual question.
I am working almost entirely with JSON formatted Objects. Most of which I need to update frequently for storage for later use. Personally I would like to avoid sending data to and from the server to rebuild the JSON set of Objects every time. So I guess in all I am wondering if there is an equivalent to something like PHP’s json_encode, json_decode? Either natively in JavaScript or through the jQuery Lib. and If not, how can I achieve something like that cause I feel (again at the moment) working with array’s vs a JSON object is much easier (correct me if I am wrong).
The reason why this question pops up in your mind is because you don’t understand neither Javascript or JSON enough.
The Javascript array is always the numerical array. There is no associate array in JavaScript like the one you get in PHP using
json_decode(json, true);. When people talking about associate array in JavaScript, they are in fact using JavaScript objects and it’s properties, i.e.,One of the best about JSON is that the syntax is a subset of the object literal notation. That means JSON string itself is valid code for representing the “associate array” in Javascript. The “conversion” couldn’t be simpler, just
eval()the string as code will do:Or, use
jQuery.parseJSON()orJSON.parse():The reason people prefer using
jQuery.parseJSON()orJSON.parse()overeval()is because eval is evil:Does the above explanation answer your question? You can find more information about JSON at http://www.json.org.