//javascript_json //json specifies that strings and key values must be enclosed in double quotes //-------------------------------------- Code 1: 'use strict' function convert(key, value) { if (typeof value === 'string') { return value.toUpperCase(); } return value; } var xiaoming = { name:'Akari', age: 19, married: false, height: '177cm', weight: '78kg', college: 'tsinghua', skills: ['java', 'javascript', 'spring', 'springmvc', 'mybatis'] } var json1 = JSON.stringify(xiaoming, null, ' '); var json2 = JSON.stringify(xiaoming, ['name', 'skills'], ' '); var json3 = JSON.stringify(xiaoming, convert, ' '); console.log(json1); console.log(json2); console.log(json3); //--------------------------------------Code 1 explanation: //1.JSON.stringify() method and serialization of objects //--------------------------------------Code 2: 'use strict' var xiaoming = { name:'Akari', age: 19, married: false, height: '177cm', weight: '78kg', college: 'tsinghua', skills: ['java', 'javascript', 'spring', 'springmvc', 'mybatis'], toJSON: function() { return { 'name': this.name, 'age': this.age }; } } var json1 = JSON.stringify(xiaoming, null, ' '); console.log(json1); //-------------------------------------- Code 2 explanation: //1. Define the 'toJSON' method in the object to control serialization //--------------------------------------Code 2: 'use strict' var json1 = '{"name": "小明", "age": 19, "married": false, "height": "177cm"}'; var obj1 = JSON.parse(json1, function(key, value) { if (value ==='Akari') { return value + 'classmate'; } return value; }) var json2 = JSON.stringify(obj1, null, ' '); console.log(json2); //-------------------------------------- Code 2 explanation: //1.JSON.parse() method and deserialization of json //--------------------------------------Code 3: 'use strict' var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%202151330&format=json'; // Get JSON from remote address: $.getJSON(url, function (data) { // get the result: var city = data.query.results.channel.location.city; var forecast = data.query.results.channel.item.forecast; var result = { city: city, forecast: forecast }; console.log(JSON.stringify(result, null, ' ')); }); //--------------------------------------Code 3 explanation: //1. Need to add jquery.js //2. Visit Yahoo's weather API to view the returned JSON data