In JSON, there are only a few data types:

  • Number: and JavaScriptnumberBe in perfect agreement;
  • Boolean: this is JavaScriptTrue or false;
  • String: is JavaScriptstring;
  • Null: is JavaScriptnull;
  • Array: is the JavaScript array representation[];
  • Object: is JavaScript{... }Representation.

Note: The JSON character set must be UTF-8. For uniform parsing, JSON strings must be quoted by “”, and Object keys must be quoted by “”.

JSON. Stringify ()

First argument: Pass only one, which converts a value (object or array) to a JSON string

The second parameter is used to filter. It can be a filter function or an array

Var data = {name: 'sunhy', info: {age: 24, sex: 'male' } } var shy = JSON.stringify(data, function (key, val) { console.log('key is %s', key) console.log('val is %s', typeof (val)) return val }) console.log(shy) // key is // val is object // key is name // val is string // key is info // val is object // key is age // val is number // key is sex // val is string // {"name":"sunhy","info":{"age":24,"sex":"male"}}Copy the code
Var data = {name: 'sunhy', info: {age: 24, sex: 'male' } } var shy = JSON.stringify(data, ['info', 'sex']) console.log(shy) // {"info":{"sex":"male"}}Copy the code

The third argument controls the spacing within the resulting string.

Can be a number, can be a string If it is a number, each level of stringification will indent the number of Spaces (up to 10 Spaces) more than the previous level;

If it is a string, each level indents the string (or the first ten characters of the string) more than the previous level.

If the third argument is not added, the output is only one line

var data = {
	name: 'sunhy',
	info: {
		age: 24,
		sex: 'male'
	}
}
var shy = JSON.stringify(data, null, 4)
console.log(shy)

// {
//    "name": "sunhy",
//    "info": {
//        "age": 24,
//        "sex": "male"
//    }
// }
Copy the code

JavaScript object => JSON string (JSON.stringify)

Var xiaoming = {name: 'xiaoming ', age: 14, gender: true, height: 1.65, grade: null, 'middle-school': '\"W3C\" Middle School', skills: ['JavaScript', 'Java', 'Python', 'Lisp'] }; var s = JSON.stringify(xiaoming, null, ' '); console.log(s)Copy the code

The results of

{" name ":" Ming ", "age" : 14, "gender" : true, "height" : 1.65, "grade", null, "middle - school" : "\"W3C\" Middle School", "skills": [ "JavaScript", "Java", "Python", "Lisp" ] }Copy the code

Partial property transformation (define onetoJSON()The method)

Var xiaoming = {name: 'xiaoming ', age: 14, gender: true, height: 1.65, grade: null, 'middle-school': '\"W3C\" Middle School', skills: ['JavaScript', 'Java', 'Python', 'Lisp'], toJSON: Function () {return {name and age only, and change key: 'name ': this.name,' age ': this.age}; }}; JSON.stringify(xiaoming); // '{"Name":" Age":14}'Copy the code

Json-formatted string => becomes a JavaScript object (JSON.parse())

JSON. Parse (' [1, 2, 3, true] '); / / [1, 2, 3, true]. JSON parse (' {" name ":" xiao Ming ", "age" : 14} '); Parse ('true'); // Object {name: 'json.parse ', age: 14} JSON. / / true JSON. Parse (' 123.45 '); / / 123.45Copy the code

Json.parse () can also accept a function to transform parsed properties

Var obj = json. parse('{"name":" small ","age":14}', function (key, value) {if (key === 'name') {return value + 'class '; } return value; }); console.log(JSON.stringify(obj)); // {name: '小 小 学', age: 14}Copy the code

Practice:

Use a browser to access the Weather API of OpenWeatherMap, view the JSON data returned, and return the city, weather forecast, and other information

Var url = 'https://api.openweathermap.org/data/2.5/forecast?q=Beijing, cn&appid = 800 f49846586c3ba6e7052cfc89af16c'; $.getjson (url, function (data) {var info = {city: data.city. Name, weather: data.list[0].weather[0].main, time: data.list[0].dt_txt }; alert(JSON.stringify(info, null, ' ')); });Copy the code