把JSON对象转换为有缩进的JSON字符串格式:
let jsonStr = '{"code":0,"msg":"OK","data":{"air_pressure":"1009","aqi":"117","city_cn":"beijing","city_code":"101010100","current_city":"北京","humidity":"90%","limit_number":"2和7","pm25":"89","temperature":"25","weather_date":"09月08日(星期五)","weather_info":"雷阵雨","weather_time":"08:40","wind_direct":"北风","wind_power":"0级"}}';
let jsonStrAfter = JSON.stringify(JSON.parse(jsonStr), null, '\t');
console.log('格式化前:', jsonStr);
console.log('格式化后: ', jsonStrAfter);
格式化结果:
参考链接:
JSON.stringify() - JavaScript | MDN
https://devpress.youkuaiyun.com/viewdesign/6407012e986c660f3cf916fd.html