当前位置:网站首页>Jason learning

Jason learning

2022-06-25 05:08:00 Lijianyi

Json:JavaScript Object Notation(JavaScript Object representation of )

Json It's a syntax for storing and exchanging text information , Its representation is similar to JavaScript Of the key/value Representation , Or object representation

var JsonString = {
     "name": "herry",
      "age": 12,
      "slogan": "dream-mack"
  };
console.log(JsonString.name);

var JsonString1 = [
    {"name": "herry", "age": 20, "slogen": "dream-top1"},
    {"name1": "monica", "age": 24, "slogen": "dream-top2"},
    {"name": "butter", "age": 28, "slogen": "dream-top3"}
];
console.log(JsonString1[0].name);

for(item in JsonString1[1]){
    console.log(item); //name1, age, slogen
}
for(val in JsonString1[1]){
    console.log(JsonString1[1][val]); //monica, 24, dream-top2
}

var JsonString2 = {
    "name": "herry",
    "age": 20,
    "solgan": {
        "solgan1": "dream-left1",
        "solgan2": "dream-left2",
        "solgan3": "dream-left3"
    }
}
console.log(JsonString2.solgan.solgan1);  //dream-left1

var JsonString3 = {
    "name": "herry",
    "age": [12, 13, 15]
}
console.log(JsonString3.age[2]); //15

Json The suffix of the file is .json file ,MIME Type bit application/json. Use directly when calling json In the previous method name, just click the specific attribute , Or use square bracket notation to get .
It can also be used directly for in perhaps forEach And so on
Delete the value and use it directly delete Delete specific key The value will do

Json Grammatical rules :
Data usage key:value Method representation of , Data is separated by commas , Braces hold objects , The brackets hold the array
Json Arrays in can be used with object methods , Achieve the purpose of nesting . It can also be used directly json Get the index in the array and modify its elements

var text = '{ "name":"Runoob", "initDate":"2013-12-14", "site":"www.runoob.com"}';
var obj = JSON.parse(text);
obj.initDate = new Date(obj.initDate);
console.log(obj.initDate);  // Sat Dec 14 2013 08:00:00 GMT+0800 ( China standard time )

var text1 = '{ "name":"Runoob", "alexa":"function () {return 10000;}", "site":"www.runoob.com"}';
var obj = JSON.parse(text1);
obj.alexa = eval("(" + obj.alexa + ")");

console.log(obj.alexa());  // 10000

json You can save numbers in (int / float), character string ,Boolean, Array , object ,null
Note here json Can't save Date object , Cannot save function , Both of these need to be converted into strings first , Turn back

stay json In which ”name”: “herry”, stay js Manifested as name: “herry”

var stringJson = '{"name": "herry", "age": 20, "solgen": "dream-top"}';
var jsonString = JSON.parse(stringJson);
console.log(jsonString.name);  //herry

var jsonString1 = {
    "name": "herry",
    "age": 20,
    "solgen": "dreame-top"
};
var stringJson1 = JSON.stringify(jsonString1);
console.log(stringJson1); // {"name":"herry","age":20,"solgen":"dreame-top"}

Use json Medium parse( ) Method to convert the data json Format file to JavaScript object

Use json Medium stringify( ) Method JavaScript Object to string , You can also convert arrays to strings

Mainstream browsers support stringify( ) Method :firefox3.5、ie8、chrome、opera10、safari4

Try not to use eval( ) Method , Prone to security problems , Use as much as possible parse( ) Method

原网站

版权声明
本文为[Lijianyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210525566853.html