3

I have a hard-coded JSON string.

var test = "JSON STRING HERE";

I am using jQuery. I know there is a function like getJSON, but that makes an AJAX call. I want it to parse the hardcoded string so that I can use $.each(test, function(a,b){}))

Thank you for your time.

Alec Smart
  • 94,115
  • 39
  • 120
  • 184

4 Answers4

11

Original Question:

jQuery makes a point of not including a publicly accessible JSON parser or encoder. They want you to use a 3rd party library for that.

I recommend the one hosted at json.org:

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Alternatively, you could use the jQuery-JSON plugin hosted on Google Code:

http://code.google.com/p/jquery-json/


In answer to "why doesn't jQuery make it's parser available?":

jQuery doesn't actually include a parser of any sort. In the AJAX section of jQuery's codebase, you can actually see what they do on lines 572-579.

The quick version is that they actually do a check to see if you've included an external JSON library such as the one from json.org, and if it finds it, they use that to parse. If you have not included one, they return the json wrapped in a function, effectively returning it for evaluation. Very tricky, but very smart!

Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
5

Do you people realize, that JSON means "JavaScript Object Notation"? If you have JavaScript Object Notation hardcoded, then just loose the quotes and you're done with parsing it as JavaScript parser will take care of that.

var jsonstr = "{prop1: 'val1', prop2: 'val2'}";
var parsed = {prop1: 'val1', prop2: 'val2'};

Easy, wasn't it?!! There might be something I'm not aware of, but for me it is quite difficult to understand people writing parsers in JavaScript for JavaScript... Sure if you're not sure about the security of source your jsonstr comes from, then evaluating it straight out of the box might not be the best idea, but in case you and only you control the source and especially if you're hardcoding it like it was said in the question, then just loose the quotes!

inkredibl
  • 1,918
  • 1
  • 14
  • 19
1

Here's what jQuery does in an ajax request when the datatype is set to "json" (which is what getJSON does, under the hood):

window["eval"]("(" + data + ")");
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
  • +1 for pointing out what jQuery does. Not for the method itself. Ugh. – eyelidlessness Sep 23 '09 at 07:25
  • That's what it eventually gets back to, though it doesn't go straight there. They actually check for an external parsing library (an object named JSON) first, then if they don't find that, they wrap it in a function call before sending it to eval. – Gabriel Hurley Sep 23 '09 at 08:09
0

Unfortunately jQuery doesn't support JSON parsing outside of the AJAX functions with JSON or JSONP as datatype (deserialization si very tightly bound to the AJAX and Callback code). You can of course just do a

var obj = eval(test);

But this is definitely not the recommened way (except for when you know for sure that your string is only a JavaScript object and not arbitrary source code to be executed). So the best way is probably to use another library like the JSON2 library (found on json.org).

Daff
  • 43,734
  • 9
  • 106
  • 120