Basic method of js operation json

  When js operates json, it is common to use [] or. to get the value of the json attribute. There are still some differences in use.

 

       https://www.cnblogs.com/wisdo/p/5905543.html

       

  JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy for machine analysis and generation. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition-December 1999). JSON uses a completely language-independent text format, but also uses habits similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, etc.). These characteristics make JSON an ideal data exchange language. There are three methods for obtaining its attribute value

 

1. Object name. Property name, similar to high-level language

var obj = '{"name":"wisdo", "age":"20"}';
var data = eval('('+ obj +')');
alert(obj.name);

2. Access by array index

var obj = '{"name":"wisdo", "age":"20"}';
var data = eval('('+ obj +')');
alert(obj[0]);  // 输出的是 name

3. Access by dictionary index

var obj = ' {"name": "wisdo", "age": "20"} ' ;
 var data = eval ( ' ( ' + obj + ' ) ' ); 
alert (obj [ ' name ' ]);   / / The output is the name value

Under normal circumstances, the first method is generally used, which is to access by object name and property name, which is very intuitive and easy to understand for programming using high-level languages, but it also has limitations, the name of the property To standardize, the key must start with a letter or underscore, including a string of letters, underscores, and numbers, and cannot start with a number.

to sum up

  data.key and data ['key'] each have their own application scenarios. Generally, data.key can be used, which is also more intuitive (it conforms to the way of accessing attributes in objects in other high-level languages); when key is a variable , And used in the loop, use data ['key'] this way.

for ( var i = 0 ; i < 10 ; i ++ ) { 
s + = data [ ' key ' + i];   // Circular call can simplify the code}

   Access by array index, although not recommended, but also has its application value; for example, when creating a map object corresponding to the id in the database, you can directly use the id value as the key, although you can give It is prefixed with a letter to make it conform to the standard of legal variable names and make its data accessible through data.key.

 

          Use [] or. To get the value of the json attribute, there are still different places. You can see below

          https://blog.csdn.net/weixin_33676492/article/details/88661517

    Access object value

    1. You can use dots to access the value of the object:

var myObj, x;
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = myObj.name;

    2. You can also use square brackets [] to access the value of an object:

var myObj, x;
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = myObj["name"];

    3. Functionally speaking, there is no difference between these two methods. But the square bracket syntax has one advantage: you can access attributes through variables, such as:

var propertyName = 'name';
alert(myObj [propertyName]);

    json needs to pay special attention when getting the value by key name.
    Assign the key name to another variable, and then get the value by. This way will not work.

var myObj, x;
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = "name";
document.getElementById("demo").innerHTML = myObj.x; // 结果是 undefined

    Can only be accessed via []:

var myObj, x;
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = "name";
document.getElementById("demo").innerHTML = myObj[x]; // 结果是 runoob

    Also, when using for traversal, you can only use myObj [x] to get the value of the corresponding attribute, not myObj.x

    In summary, when the key name is a variable, you can only use [] to obtain the corresponding attribute value.
    and also! If the attribute name contains characters that cause syntax errors, or if the attribute name is a keyword or reserved word, square brackets are also used. Such as:

var response = {
"awards":{'105':50, '107':10,'108':5,'110':3,'111':2, '112':1}
};
console.log(response.awards['105']) //50
console.log(response.awards.105) // error
 

    Cycle object

    1. You can use for-in to cycle the attributes of the object: key

var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) { document.getElementById("demo").innerHTML += x + "<br>"; }

    2. ** When for-in looping the attributes of an object, use square brackets ([]) to access the value of the attribute:
    when using for for traversal, the value of the corresponding attribute can only be obtained through myObj [x], not Use myObj.x **

var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) { document.getElementById("demo").innerHTML += myObj[x] + "<br>"; }
 

    Nested JSON object value can be a legal JSON data type

    1. The JSON object can contain another JSON object:

myObj = { 
"name":"runoob", "alexa":10000,        "sites": { "site1":"www.runoob.com", "site2":"m.runoob.com" }   }

    2. You can use dots (.) Or square brackets ([]) to access nested JSON objects.

x = myObj.sites.site1; // 或者 x = myObj.sites["site1"];

    Modify value

    1. You can use the dot (.) To modify the value of the JSON object:

myObj.sites.site1 = "www.google.com";

    2. You can use square brackets ([]) to modify the value of the JSON object:

myObj.sites["site1"] = "www.google.com";

    Delete object properties

    1. We can use the delete keyword to delete the properties of the JSON object:

delete myObj.sites.site1;

    2. You can use square brackets ([]) to delete the attributes of the JSON object:

delete myObj.sites["site1"]

 

Guess you like

Origin www.cnblogs.com/lnlvinso/p/12756178.html