Fastest way to parse json strings (without jquery)


can someone tell me the fastest way to parse a json string for an object without jquery? I want to parse the json string in a script tag before loading jquery.

Thanks in advance!
Peter

+6
source share
4 answers
+9
source

To convert JSON text to an object, you can use the eval () function. eval () calls the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will parse the text correctly and create the structure of the object. The text should be wrapped in parens to avoid disabling ambiguity in JavaScript syntax.

var myObject = eval('(' + myJSONtext + ')'); 
+6
source
 var myObject = eval('(' + myJSONtext + ')'); 
+2
source

If the JSON string comes from the server, you can try JSONP . JSON is parsed initially in the browser (quickly) upon loading and without any library.

for example: if you have the answer {"name":"Peter"}

The JSONP answer would be something like this: yourFunction({"name":"Peter"})

yourFunction should be a globally defined function on the page that will receive the call, for example:

 function yourFunction(json){ //do something with the JSON } 
+1
source

Source: https://habr.com/ru/post/893047/


All Articles