json转html
发布网友
发布时间:2022-05-07 18:48
我来回答
共2个回答
热心网友
时间:2022-05-13 16:59
function jsonclick() {
var jsonstr = '{"desc":"OK","status":1000,"data":{"wen":"34","ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","forecast":[{"fengxiang":"东风","fengli":"3-4级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"22日星期一"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"23日星期二"},{"fengxiang":"东风","fengli":"微风级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"24日星期三"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"25日星期四"},{"fengxiang":"东北风","fengli":"3-4级","high":"高温 33℃","type":"阴","low":"低温 25℃","date":"26日星期五"}],"yesterday":{"fl":"3-4级","fx":"北风","high":"高温 33℃","type":"雷阵雨","low":"低温 27℃","date":"21日星期日"},"aqi":"53","city":"常州"}}';
var list = new Function("return" + jsonstr)();
//html代码随便拼凑成需要的格式就行了
var htmlstr = "<ul style='border:1px solid #aaccff; height:100px; width:600px;'> <li> 状态码:"
+ list.status + "</li> <li> 日期:"
+ list.data.yesterday.date + "</li> <li> 地址:"
+ list.data.city + "</li> <li> 预测风向:"
+ list.data.forecast[0].fengxiang + "</li> </ul>";
document.getElementById("id").innerHTML = htmlstr;
}