Into the javascript series (15) -JSON

A data format, not a programming language

{
	'name': 'json',
	'age':15
	'v' : [
		'simpleValue',
		'object',
		'array'
	]
}

grammar

Contains values ​​of three data types

  • Simple values: string type, numeric type, boolean, null
  • Object: represents {} complex data object
  • Array: indicates that [] can be a collection of simple values, a collection of objects, or a collection of arrays

Parsing and serialization

JSON object

  • JSON.stringify () converts the object to a json string
  • JSON.parse () serializes json strings into objects

Serialization options

JSON.stringify(obj, [attr1, attr2...]); 过滤出某些属性 
JSON.stringify(obj, function(key, value) {
	// 按一定逻辑规则渲染原对象的值
});
JSON.stringify(obj, null, 4); // 第三个参数缩进字符
可以重写对象的toJSON方法
  • toJSON () can be used as a complement to function filters, so it is important to understand the internal order of serialization.
  • Assuming that an object is passed into JSON.stringify (), the sequence of serializing the object is as follows
  • (1) If there is toJSON () method and can get valid value through it, then call this method. Otherwise, the object itself is returned.
  • (2) If the second parameter is provided, apply this function filter. The value passed into the function filter is the value returned in step (1).
  • (3) Serialize each value returned in step (2) accordingly.
  • (4) If the third parameter is provided, perform the corresponding format.

JSON.parse () also has a second parameter

JSON.parse(jsonText, function(key, value) {
	// xxx
});

Fastjson under JAVA syntax can refer to
https://www.cnblogs.com/jajian/p/10051901.html

Guess you like

Origin www.cnblogs.com/pengsn/p/12742946.html