Home Web Front-end JS Tutorial Detailed explanation of conversion between JSON strings and objects_json

Detailed explanation of conversion between JSON strings and objects_json

May 16, 2016 pm 03:57 PM
json string object

JSON (JavaScript Object Notation) is a subset of the JavaScript programming language. Because JSON is a subset of JavaScript, it can be clearly used in this language.

eval function Convert JSON text to object

In order to convert JSON text into an object, you can use the eval function. The eval function calls the JavaScript editor. Since JSON is a subset of JavaScript, the compiler will correctly parse the text and produce object structures. Text must be enclosed in parentheses to avoid JavaScript syntax ambiguities.
var obj = eval('(' JSONTest ')'); The eval function is very fast. It can compile and execute any JavaScript program, thus creating security issues. The eval function should only be used when using trusted and complete source code. This allows for safer parsing of JSON text. For web applications that use XmlHttp, communication between pages only allows the same origin, so it can be trusted. But it is not perfect. If the server does not have strict JSON encoding, or does not have strict input validation, it may transmit invalid JSON text including dangerous scripts. The eval function will execute the malicious script.

JSON interpreter JSON.parse, JSON.stringify

Using a JSON parser can prevent security risks like the eval function that converts JSON text into objects. The JSON parser can only recognize JSON text and reject all scripts. Browsers that provide native JSON support will have their JSON parsers much faster than the eval function.

Currently, Firefox, Opera, and IE8 and above also provide local JSON support. Among them, the functions provided by the JSON interpreter are: JSON.parse, JSON.stringify.

For browsers that do not provide native JSON support, you can introduce the script json2.js to implement the JSON conversion function. The json2.js script can be downloaded from the https://github.com/douglascrockford/JSON-js/blob/master/json2.js page.

JSON.parse function

Convert JSON text to object.
JSON.parse(text[, reviver])
Parameters
text
Required. JSON text to be converted to an object.
reviver
Optional. This parameter is a replacement function. In the transformation, for each node traversed, this function will be executed, and the return value of the function will replace the corresponding node value of the transformation result.

JSON.stringify function

Convert object to JSON text.
JSON.stringify(value[, replacer[, space]])
Parameters
text
Required. The object to be converted to JSON text.
reviver
Optional. This parameter is a replacement function. In the transformation, for each node traversed, this function will be executed, and the return value of the function will replace the corresponding node value of the transformation result.
space
Optional. The number of spaces to indent the formatted output JSON text. If this parameter is not provided the output will not be formatted.
Delegate type of parameter reviver
reviver(key, value)
This in the reviver function is the parent node of the node currently traversed. When the root node is traversed, the parent node is an Object object, the root node is an attribute of the object, and the attribute name is an empty string.
Parameters
key
When the parent node is an array Object, the key is the array index, otherwise the key is the Object property name.
value
node value.
Note: JSON does not support circular data structures.

jQuery.parseJSON( jsonTex )

jQuery also has a method for converting strings to JSON format, jQuery.parseJSON(json), which accepts a standard format JSON string and returns a parsed JavaScript (JSON) object. Of course, if you are interested, you can encapsulate a jQuery extension yourself. jQuery.stringifyJSON(obj) converts JSON into a string.

The above is the entire content of this article. I hope you all like it.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

As a scripting language widely used to develop web applications, PHP has very powerful string processing functions. In daily development, we often encounter operations that require deleting a string, especially the last two characters of the string. This article will introduce two PHP techniques for deleting the last two characters of a string and provide specific code examples. Tip 1: Use the substr function The substr function in PHP is used to return a part of a string. We can easily remove characters by specifying the string and starting position

See all articles