Home > Web Front-end > JS Tutorial > Some small experiences I have summarized, operating JSON in JS

Some small experiences I have summarized, operating JSON in JS

php是最好的语言
Release: 2018-07-23 15:17:35
Original
1091 people have browsed it

The commonly used JSON processing methods are briefly summarized. JSON is a lightweight data exchange format that uses a completely language-independent text format and is an ideal data exchange format. The following is the specific operation method of its implementation.

1. Overview

JSON (JavaScript Object Notation) is a lightweight data exchange format that uses a completely language-independent text format, which is ideal Data exchange format. At the same time, JSON is a JavaScript native format, which means that processing JSON data in JavaScript does not require any special API or toolkit.

In JSON, there are two structures: objects and arrays.

1.Object

An object starts with "{" and ends with "}". Each "key" is followed by a ":", and "'key/value' pairs" are separated by ",".

packJson = {"name":"nikita", "password":"1111"}

2.Array

packJson = [{"name":"nikita", "password":"1111"}, {"name":"tony", "password":"2222"}];

The array is An ordered collection of values. An array starts with "[" and ends with "]". Use "," to separate values.

2. Conversion of JSON objects and JSON strings

1. In the data transmission process, json is transmitted in the form of text, that is, a string, and JS operates on JSON objects, so the conversion between JSON objects and JSON strings is the key. For example:

JSON string:

var jsonStr = '{"name":"nikita", "password":"1111"}';

JSON object:

var jsonObj = {"name":"nikita", "password":"1111"};

2. Convert String to Json


var myObject = eval('(' myJSONtext ')');


eval is a function that comes with js, it is not very safe, you can consider it Use json package.

3. TraverseJSONObject

myJson = {"name":"nikita", "password":"1111"( p  myJson){
+ " " +
Copy after login

Running result:

Some small experiences I have summarized, operating JSON in JS

4. TraverseJSONArray

packJson ="name":"nikita", "password":"1111""name":"tony", "password":"2222"( p  packJson){
+ " " +
Copy after login

I prefer this way of writing:


( i = 0; i < packJson.length; i+++ " " +
Copy after login


Run result:

Some small experiences I have summarized, operating JSON in JS

Five, combine twoJSONObjects are assembled into one

(targetJson &&( p =
Copy after login

The usage is as follows:


##

 json1 = {"name":"nikita" json2 = {"password":"1111"
Copy after login


Run results:

Some small experiences I have summarized, operating JSON in JS

Please indicate the address of this article when reprinting: Some small experiences summarized by yourself, operating JSON in JS (reprint)

Related recommendations:

PHP JSON operation example analysis

##PHP JSON operation, phpjson operation_PHP tutorial

PHP JSON operation, phpjson operation

The above is the detailed content of Some small experiences I have summarized, operating JSON in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template