====json.parse
Json.parse (Text,callback (k,v)) method parses a JSON string
Return value: Object (corresponding text text)
Parameters:
Text JSON string to parse
Callback (K,V) optional callback function//specifies how the original value is parsed before being returned
Example 1:
Json.parse (' {} '); // {}
Json.parse (' true '); True
Json.parse (' "foo"); "Foo"
Json.parse (' [6, 6, ' hello '] '); [6, 6, "Hello"]
Json.parse (' null '); Null
JSON.parse()
不允许用逗号作为结
尾
JSON.parse("[1, 2, 3, 4, ]")//
错误
JSON.parse(‘{"foo" : 1, }‘)//
错误
JSON
格式的字符串
,
必须遵循
JSON
规范
,key
和
value
都以引号引起来
(
而且
,
外部用单引号
,
内部用双引号
)//
否则解析不出来会报错
var person_json=JSON.parse(‘{"name":"jack","age":"19","phone":"182"}‘)
console.log(person_json["name"])
Example 2: (Basic Good mastery)
2.1
var obj=json.parse (' {' P ': 5} ', function (k, v) {
if (k = = = ") return V; If the top level is reached, the property value is returned directly,
Return v * 2; Otherwise, the property value becomes twice times the original.
});
Console.log (obj)
{P:10}
2.2
Json.parse (' {"1": "Hello", "2": 2, "3": {"4": 4, "5": {"6": 6}} ', function (k, v) {
Console.log (k); Output the current property name to know that the traversal order is from inside out,
The last property name will be an empty string.
return v; Returns the original property value, which is equivalent to not passing the callback parameter.
});
If a function is specified 回调
, the parsed object value (the parsed value) is converted to the final return (return value). More specifically, the parse value itself and all of the properties it contains, will be in a certain order (starting from the innermost attribute, first level outward, eventually reaching the top layer, that is, the parsing value itself) to call the 回调
function, in the call process, the current property belongs to the object as a this
value, The current property name and property values are passed in as the first and second arguments, respectively 回调函数
. If 回调
returned undefined
, the current property is removed from the owning object, and if other values are returned, the returned value becomes the new property value of the current property.
When traversing to the topmost value (parsing value), the arguments passed into 回调
the function are an empty string ""
(because there is no real property at this time) and the current parsed value (which may have been modified), the current this
value will be{"":
修改过的解析值}
====
JSON.stringify
JSON.stringify(value,replacer,space )
方法将对象转换为
JSON
字符串
返回值
:json
字符串
参数
:
value:
将要序列化的对象
后面两个可选参数
,
基础好的掌握
:
replacer:
①如果该参数是一个函数,则在序列化过程中,被序列化的值的每个属性都会经过该函数的转换和处理;注
:
这个函数第一次调用的时候
replacer(k,v) k
值是没有的
,v
值就是对象自己
,
后面调用传入的是对象内部的属性
,
并且按照属性名的
ASCII
表的顺序来分别调用
.
如果返回一个
Number,
转换成相应的字符串被添加入
JSON
字符串。
//return 5 “5”
如果返回一个
String,
该字符串作为属性值被添加入
JSON//return “hello” “hello”
如果返回一个
Boolean, "true"
或者
"false"
被作为属性值被添加入
JSON
字符串。
如果返回任何其他对象,该对象递归地序列化成
JSON
字符串,对每个属性调用
replaceer
方法。除非该对象是一个函数,这种情况将不会被序列化成
JSON
字符串。
如果返回
undefined
,该属性值不会在
JSON
字符串中输
例
:
对象中属性值为字符串的属性除外
,
将这个对象变为
json
格式的字符串
var obj={“name”:”karen”,”age”:46,”beatiful”:true,”tel”:”136xxx”}
function replacer(k,v){
if(typeof(v)===’string’){return undefined}
return v
}
var jsonStr=JSON.stringify(obj,replacer)
②如果该参数是一个数组,则只有包含在这个数组中的属性名才会被序列化到最终的
JSON
字符串中;
var obj={“name”:”karen”,”age”:46,”beatiful”:true,”tel”:”136xxx”}
var arr=[‘age’,’tel’]
var jsonStr=JSON.stringify(obj,arr)
③如果该参数为
null
或者未提供,则对象所有的属性都会被序列化;
Space:
用来控制结果字符串里面的间距。如果是一个数字
,
则在字符串化时每一级别会比上一级别缩进多这个数字值的空格(最多
10
个空格);如果是一个字符串,则每一级别会比上一级别多缩进用该字符串(或该字符串的前十个字符
)
var obj=JSON.stringify({ name: 1, age : 2 }, null,0)
alert(obj)
var obj=JSON.stringify({ name: 1, age : 2 }, null,10)
alert(obj)
var obj=JSON.stringify({ name: 1, age : 2 }, null,”heihei”)
alert(obj)
= = = Symbol Priority
1---
Parentheses---()
2--
Member access (left to right)---p1.name
New with parameter---new person ()
3----
Function call (left to right)----FN (10)
New No parameter---new person
4----
Post-Increment---a++
Post-decrement----a--
5----
Logical non (right-to-left)----! true
Bitwise non (right-to-left)----
unary addition (right to left)-----20
Unary subtraction (right-to-left)----+0
Pre-increment (right-to-left)----++a
Pre-decrement (right-to-left)-------A
Typeof (right to left)-----Typeof p1
6----
Multiply (from left to right)----20*10
Except (left to right)----A/2
Redundancy (right to left)----20%3
7----
Plus (left to right)----a+b
Minus (left to right)----A-B
8----
Less than (left to right)----a<b
Less than or equal to (left to right)----a<=b
Greater than (left to right)----a>b
Greater than or equal to (left to right)----a>=b
In (left to right)----for (k in Arr)
Instanceof (from left to right)----p1.instanceof (person)
9----
Equal sign (left to right)----a==b
Non-equal (left-to-right)----a!=b
Full Equals (left to right)----a===b
Non-full equals (left to right)---a!==b
Ten----
Bitwise-and (left-to-right)----2&3
---
Bitwise XOR (left-to-right)----10^20
---
Bitwise OR (left-to-right)----10|10
---
Logical AND (left-to-right)----true&&true
----
Logical OR (left-to-right)----false| | True
---
Trinocular operator (right-to-left)----A?B:C
---
Assignment (right to left)----a=20
----
Comma (left to right)----for (Var a=20;a++,a++,a++,a<10;a++) {}
js-supplemental-json, Symbol priority