May 08, 2021 JSON
One of the most common uses of JSON is to read JSON data (as a file or as HttpRequest) from a web server, convert JSON data to a JavaScript object, and then use that data in a Web page.
To make it easier for you to explain, we use strings as inputs (not files).
Create a JavaScript string with JSON syntax:
Because the JSON syntax is a subset of the JavaScript syntax, the JavaScript function eval() can be used to convert JSON text to JavaScript objects.
The eval() function uses a JavaScript compiler that parses JSON text and then generates JavaScript objects. The text must be surrounded in parentheses to avoid syntax errors:
Use JavaScript objects in Web pages:
The eval() function compiles and executes any JavaScript code. This hides a potential security issue.
Using the JSON parser to convert JSON to JavaScript objects is more secure. The JSON parser recognizes only JSON text, not compiles scripts.
In the browser, this provides native JSON support, and the JSON parser is faster.
Native support for JSON is included in the newer browsers and the latest ECMAScript (JavaScript) standards.
Web browser support | Web software support |
---|---|
|
|
For older browsers, you can use the JavaScript Library: https://github.com/douglascrockford/JSON-js