JSON (Java Script Object Notation) using JavaScript, is a format for storing and transmitting data, the server typically used to pass data to the page. JSON format is only a text, the text can be read in any programming language, and as the format of transmission data (js string in a similar format object, which is a string JSON, but long objects like) from the above description, we can see the following knowledge
JSON rule: data in a pair of quotation marks, with {} or [] enclosed; for each data which is expressed in a key-value pair (key: value); key must be placed in double quotes, not single quotes : each data separated by a comma.
Js for interacting with the rear end of the data: JSON use
JSON attribute value: only 10 hexadecimal values, strings (double quotes), Boolean, and null, an array, in line with the requirements of the object JSON (implication is not a function, NaN, + (-) Infinity and undefined).
< Script >
// JSON array format
var ARR = [ ' Davina ' , ' Amy ' , ' Lisa ' ]; // array to store strings
the console.log (ARR [ 2 ]); // Lisa
was arr1 = [scars,
{
"name": "dvaina",
"age": 18
},
{
"name": "lisa",
"age": 20
}]
console.log(typeof arr1, arr1[2].name); //object lisa
</script>
JSON object methods: stringify () and parse ().
The JSON.stringify () method of an object into a JSON-formatted string, if the object is not quoted in the key, but also can be converted, the converted result will automatically give it quotation marks.
A parameter of the situation:
<script>
var obj1 = [1, 2, 3];
var obj2 = { "name": 'dvaina', "age": '20' };
var obj3 = { name: 'lisa', "age": '20' };
console.log(JSON.stringify(obj1)); //[1,2,3]
console.log(JSON.stringify(obj2)); //{"name":"dvaina","age":"20"}
console.log(JSON.stringify(obj3)); //{"name":"lisa","age":"20"}
// the stringify () method to convert an object into a mathematical object word string
the console.log (the JSON.stringify (the Math)); // {}
the console.log (the JSON.stringify ( new new Boolean ( to true ))); / / to true
// the stringify () method converts the object into a string date
the console.log (the JSON.stringify ( new new a date ())); // "2019-12-09T06: 22 is: 34.685Z"
// conversion, or if the object member is an undefined function member will ignore
// If a member of the array is undefined or a function, the value is converted to null
OBJ4 = {
a: function () { },
b: undefined,
c: [function () { }, undefined],
}
console.log(JSON.stringify(obj4)); //{"c":[null,null]}
</script>
The second parameter may be a function or an array
<script>
//如果第二个参数是函数,序列化过程中的每个属性都会被这个函数转化和处理
var str = {
"name": "davina",
"age": 18,
"phone": 11111111
}
var jsonStr = JSON.stringify(str, function (key, value) {
if (key == "phone") {
return "(0713)" + value;
} else if (typeof value === "age") {
return value + 2;
} else {
return value;
}
})
console.log(jsonStr) //{"name":"davina","age":18,"phone":"(0713)11111111"}
//如果是数组,那只有包含在这个数组中的属性才会被序列化到json字符串中
var str1 = {
"name": "davina",
"age": 18,
"phone": 11111111
}
var jsonStr1=JSON.stringify(str1,["name","age","phone","address"]);
console.log(jsonStr1); //{"name":"davina","age":18,"phone":11111111}
//如果是null,那和空一样
</script>
第三个参数用于美化输出——不建议用,忽略。
JSON.parse()将字符串转化成对象,如果传入的不是有效JSON格式,会报错。JSON.parse()也可以接收一个函数参数,在每个键值对上调用,这个函数被称为还原函数,该函数接收两个参数,一个键和一个值。如果还原函数返回undefined,那就要从结果中删除相应的键,如果返回其它值,则将值插入到结果中。
<script>
console.log(JSON.parse('{}')); //{}
console.log(JSON.parse('true')); //true
var o = JSON.parse('{"name":"davina"}');
console.log(o.name); //dvaina
//JSON.parse()也可以接收一个函数参数,在每一个键值对调用,这个函数被称为还原函数,这个函数接收两个参数,一个键和一个值,返回一个值
var book = {
"name": "js",
"date": new Date(2019, 9, 1)
}
var jsonStr = JSON.stringify(book);
console.log(jsonStr)//{"name":"js","date":"2019-09-30T16:00:00.000Z"}
var bookName = JSON.parse(jsonStr, function (key, value) {
if (key == 'date') {
return new Date(value);
}
return value;
})
console.log(bookName.date.getFullYear());//2019
</script>
eval()类似于JSON.parse()方法,可以将json字符串转换成json对象,但是它也可以执行不符合json格式的代码,所以尽量少用或不用。