This series primarily records JavaScript, where beginners are more likely to be mistaken.
(1) JSON
JSON is the abbreviation for JavaScript Object notation, which is a data interchange format.
Before JSON appeared, everyone used XML to pass the data. Because XML is a plain text format, it is suitable for exchanging data on the network. XML itself is not complicated, but, with a lot of complex specifications such as DTDs, XSD, XPath, XSLT, and so on, any normal software developer will feel a big head when it comes to XML, and finally we find that even if you work hard for a few months, you may not be able to understand the specification of XML.
Finally, on a day in 2002, Grass Crockford Douglas Crockford, a software engineer who had been fooled by a handful of giant software companies in order to save a deep abyss, invented the ultra-lightweight data Interchange format of JSON.
As a senior architect of Yahoo, Douglas has a natural fondness for JavaScript. The JSON he designed is actually a subset of JavaScript. In JSON, there are a total of several data types:
- Number: Exactly the same as JavaScript
number
;
- Boolean: is JavaScript
true
or false
;
- String: It's JavaScript
string
;
- Null: it is JavaScript
null
;
- Array: That's the
Array
way JavaScript is represented-- []
;
- Object: Is the way JavaScript is
{ ... }
represented.
And any combination of the above.
And, JSON is dead. The character set must be UTF-8, which means that there is no problem with multiple languages. For unified parsing, the JSON string must be in double quotes ""
, and the key of object must also be double-quoted ""
.
Because JSON is very simple, it quickly swept the web world and became the ECMA standard. Almost all programming languages have libraries that parse JSON, and in JavaScript we can use JSON directly, because JavaScript has built-in JSON parsing.
To turn any JavaScript object into JSON is to serialize the object into a JSON-formatted string so that it can be passed to other computers over the network.
If we receive a string in JSON format, we just need to deserialize it into a JavaScript object, and we can use it directly in JavaScript.
Serialization of
Let's first serialize this object into a JSON-formatted string:
var xiaoming = { name: ‘小明‘, age: 14, gender: true, height: 1.65, grade: null, ‘middle-school‘: ‘\"W3C\" Middle School‘, skills: [‘JavaScript‘, ‘Java‘, ‘Python‘, ‘Lisp‘]};JSON.stringify(xiaoming); // ‘{"name":"小明","age":14,"gender":true,"height":1.65,"grade":null,"middle-school":"\"W3C\" Middle School","skills":["JavaScript","Java","Python","Lisp"]}‘
To output a good look, you can add parameters to the output by indentation:
null, ‘ ‘);
Results:
{ "name": "小明", "age": 14, "gender": true, "height": 1.65, "grade": null, "middle-school": "\"W3C\" Middle School", "skills": [ "JavaScript", "Java", "Python", "Lisp" ]}
The second parameter controls how the object's key value is filtered, and if we only want to output the specified property, we can pass in Array
:
JSON.stringify(xiaoming, [‘name‘, ‘skills‘], ‘ ‘);
Results:
{ "name": "小明", "skills": [ "JavaScript", "Java", "Python", "Lisp" ]}
You can also pass in a function so that each key-value pair of an object is processed first by the function:
function convert(key, value) { if (typeof value === ‘string‘) { return value.toUpperCase(); } return value;}JSON.stringify(xiaoming, convert, ‘ ‘);
The above code capitalizes all attribute values:
{"name": age ": 14," gender ": true, "height": 1.65, "grade": null, " Middle-school ": " \ "w3c\" Middle School "," skills " : [ "JAVASCRIPT", "JAVA", " PYTHON ", " LISP "]}
If we also want to control exactly how to serialize xiaoming, you can give xiaoming
a method that defines a ToJSON ()
, which directly returns the data that the JSON should serialize:
var xiaoming = {name: ' xiaoming ', Age: 14, Gender: true, Height: 1.65, Grade: null, ' Middle-school ': ' Java ', ' Python ', function () {return {//only output Name and age, and changed key: ' Name ': this.name, ' age ': this.age}; }}; Json.stringify (xiaoming); //' {"Name": "Xiaoming", "Age": +} '
Deserialization
To get a string in JSON format, we use JSON.parse()
it to turn it into a JavaScript object:
JSON.parse(‘[1,2,3,true]‘); // [1, 2, 3, true]JSON.parse(‘{"name":"小明","age":14}‘); // Object {name: ‘小明‘, age: 14}JSON.parse(‘true‘); // trueJSON.parse(‘123.45‘); // 123.45
JSON.parse()
You can also receive a function that transforms the parsed property:
JSON.parse(‘{"name":"小明","age":14}‘, function (key, value) { // 把number * 2: if (key === ‘name‘) { return value + ‘同学‘; } return value;}); // Object {name: ‘小明同学‘, age: 14}
Using JSON in JavaScript is so easy!
JavaScript Basics Collection _json (eight)