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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
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 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.

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 do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

Detailed explanation of Golang string modification: dynamic adjustment and variability Detailed explanation of Golang string modification: dynamic adjustment and variability Apr 08, 2024 pm 03:27 PM

Strings in GoLang, although immutable, can be dynamically modified using the following technique: concatenating strings using string concatenation. Create a new string using string formatting. Modify the underlying byte slice of the string. Use mutable string types provided by third-party libraries.

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips Mar 28, 2024 pm 03:02 PM

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips In PHP development, string processing is a very common requirement. Sometimes we need to process the string to remove extra commas and retain the only commas. In this article, I'll introduce an implementation technique and provide concrete code examples. First, let's look at a common requirement: Suppose we have a string containing multiple commas, and we need to remove the extra commas and keep only the unique comma. For example, replace "apple,ba

See all articles