The 10 minute treatment js common method json

A, json defined

JSON (JavaScript Object Notation), which is just a bunch of string elements will be marked with specific symbols.

  • Bis {} brackets indicate the object
  • [] Represents an array in brackets
  • "" Is in quotation marks or attribute value
  • : Colon which indicates the value of the former (this value may be a string, a number, or may be another array or object)

JavaScript Object Notation JSON syntax is a subset of the syntax of law.
Data in the name / value pairs separated data by commas, braces for objects, brackets backup array

JSON values may be: a number (integer or floating point), the string (in double quotes), a logical value (true or false ), array (in square brackets), the object (in braces), null

Therefore, { "name": "Michael"} may be understood as a name for the object containing the Michael

Second, why use json

  1. JSON is a text format that is independent of language and platform.
  2. To generate and parse XML in terms of relative simplicity.
  3. Read and write faster.

Three, JS built two Json method

1, the object into a string

Any Json into the JavaScript, this object is serialized as Json string before it can be transmitted through the network; 

JSON.stringify( {} , [ ] , "")

 

// Parameter One: To serialized data (Object) 
// two parameters: the control key object, just the output of the specified attribute, passed in an array 
@ three parameters: After serialization, the printout format (a Tab, can be more intuitive view json)

   example:

 

2, the string into an object

If we receive a JSON string format, just take it deserialized into a JavaScript object, you can use the object directly in JavaScript

JSON.parse( json.DATA )  

// incoming json string

   

Fourth, the global replacement string

Here we need to implement replaceAll function with JS, all the 'xa "' replaced '' '.
Code below, gm is fixed here, g represents a global, m represents a multiple, regular expressions may be used

 

 V. traverse json object and the json array

1 traversing JSON object code as follows:

packJson = {var "name": "Liza", "password": "123"}; 
for (var K in packJson) {// iterate each key object packJson / value pair of, k is the key 
Alert (K + " "packJson + [K]); 
}

2, JSON array traversal code is as follows:

var packJson = [{"name":"Liza", "password":"123"}, {"name":"Mike", "password":"456"}];
for(var i in packJson){//遍历packJson 数组时,i为索引
alert(packJson[i].name + " " + packJson[i].password);
}

 

 

   

 This is a constant learning imitation, practice, and then to finally face his own original process.

虽然可能从来不能写出超越网上通类型同主题博文,但为什么还是要写?
于自己而言,博文主要是自己总结。假设自己有观众,毕竟讲是最好的学(见下图)。

于读者而言,笔者能在这个过程get到知识点,那就是双赢了。
当然由于笔者能力有限,或许文中存在描述不正确,欢迎指正、补充!
感谢您的阅读。如果本文对您有用,那么请点赞鼓励。

  

 



 

Guess you like

Origin www.cnblogs.com/zishengY/p/10994621.html