The difference between Vue JSON array, JSON object and array

1. JSON syntax rules :

JSON is a syntax for storing and exchanging textual information

During the development process, it is often necessary to exchange data with other systems. The formats of data exchange include XML, JSON, etc.

As a lightweight data format, JSON is more efficient than XML. XML requires a lot of tags, which undoubtedly occupies network traffic.

1. JSON can have two formats

One is in object format:

{"name":"JSON","address":"Beijing Xicheng District","age":25}//JSON object format string

The other is an array object

[{"name":"JSON","address":"Beijing Xicheng District","age":25}]//data object format

From the above two formats, it can be seen that the only difference between the object format and the array object format is that [] is added on the basis of the object format. Looking at the specific structure, it can be seen that they all appear in the form of key-value pairs. , separated by a comma (,) in English.

This format is also very popular when transferring data between the front end and the back end. The back end returns a string in json format. The front end uses the JSON.parse() method in js to parse the JSON string into a json object, and then proceeds Traversal, for front-end use.

JavaScript objects represent a subset of the syntax.

  1. Data is in name/value pairs

  2. data separated by commas

  3. Curly braces hold objects

  4. Square brackets hold arrays

2. JSON name/value pairs

  1. The writing format of JSON data is: name/value pair.

  2. In name/value pair combinations, the name is written first (in double quotes), and the value pair is written after (also in double quotes), separated by a colon:

  3. "firstName":"John"

3. JSON value

Values ​​can be:

  1. number (integer or float)

  2. string (in double quotes)

  3. logical value (true or false)

  4. array (in square brackets)

  5. object (in curly braces)

JSON is normally composed of objects and arrays, and objects are manipulated using object methods. Arrays are manipulated using array methods.

 

array

  1. var ourcountry=[["Beijing"],["Shanghai"],["Hefei","Wuhu","Bengbu"]]; // array alert(ourcountry[2][1])
    ;

JSONArray

  json array, use square brackets [ ], but the items in the array are also in json key-value pair format

JSON object

 A json object is a key corresponding to a value, using braces { }, such as: {key:value}

var status_process = {                 "name5" : 'idle period',                 "name1" : 'sowing period',                 "name2" : 'seedling period',                 "name3" : 'growing period',                 "name4" : 'harvesting period' alert(status_process["name5"]); alert(status_process.name5); // both are: idle period JSON string









The so-called json string means that the value of the string variable is in the same format as json, but it is not a json object, for example:

var s1="{";
var s2 = " 'name5' : 'idle period', 'name1' : 'sowing period','name2' : 'seedling period','name3' : 'growing period', 'name4' : 'Harvest period'";
var s3="}";
var status_process=s1+s2+s3;

JSON array traversal

packJson = [
 
        {"name":"zhang", "password":"123"},
 
        {"name":"li", "password":"456"}
 
];
 
for(var p in packJson){// When traversing the json array, write p as the index
 
    alert(packJson[p].name + " " + packJson[p].password);
 
}
 
// or
for(var i = 0; i < packJson.length; i++){         alert(packJson[i].name + " " + packJson[i].password); } JSON array operations
 


1. Define var josnArry=[];

2. Add jsonArry.push(jsonObj);

3. Delete delete jsonArry[0]; splice(index,length):

4. Loop through the array

a.jquery

$.each(jsonArry,function(index,item){     var userId=item.userId;           }); b. General method


for(var i=0;i<jsonArry.length;i++)
{     var userId=jsonArry[i].userId;  } c.special method


for(var item in jsonArry)

    var userId=jsonArry[item].userId;//Note: The item here is not an array item, but the index of the array item
}


JSON object traversal

myJson = {"name":"ji", "password":"123"};
 
for(var p in myJson){
//Traverse each key/value pair of the json object, p is the key     alert(p + " " + myJson[p]); }
 

 

Convert between string and json object and json object array
var jsonStr = "{\"userId\":\"001\"}"; // json object string
var jsonArryStr = "[{\"userId\":\" 001\"},{\"userId\":\"002\"}]"; // json array string
var jsonObj = JSON.parse(jsonStr); // string converted to json object
var jsonArry = JSON. parse(jsonArryStr); // convert string to json array
var jsonStr = JSON.stringify(jsonObj); // convert json object to string
var jsonArryStr=JSON.stringify(jsonArry);// convert json array to string
alert (jsonStr);
alert(jsonArryStr);
alert(jsonObj. userId);
alert(jsonArry[0]['userId']);

JSON.stringify(obj) Convert JSON object to string.
JSON.parse(string) Convert a string to JSON object format.

 

Convert json string to json object

 
  1. // jquery的方法

  2. var jsonObj = $.parseJSON(jsonStr)

  3. //js 的方法

  4. var jsonObj = JSON.parse(jsonStr)

Convert json object to json string

  1. //js方法

  2. var jsonStr1 = JSON.stringify(jsonObj)


Note: Only arrays or json arrays can use the length method. The length method of the json object is not valid.

  Map map and json are both key-value pairs. The difference is that the key-value pairs in map are separated by equal signs, and the key-value pairs in json are separated by colons. In fact, json is a special form of map.

Guess you like

Origin blog.csdn.net/Youning_Yim/article/details/109677372