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){ + " " +
Running result:
4. TraverseJSONArray
packJson ="name":"nikita", "password":"1111""name":"tony", "password":"2222"( p packJson){ + " " +
I prefer this way of writing:
( i = 0; i < packJson.length; i+++ " " +
Run result:
Five, combine twoJSONObjects are assembled into one
(targetJson &&( p =
The usage is as follows:
##
json1 = {"name":"nikita" json2 = {"password":"1111"
Please indicate the address of this article when reprinting: Some small experiences summarized by yourself, operating JSON in JS (reprint)
PHP JSON operation example analysis
##PHP JSON operation, phpjson operation_PHP tutorialPHP JSON operation, phpjson operationThe 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!