下面是我的字符串
jsonStr ="[{\"大家电\":[{\"one\":\"电视机\"},{\"two\":\"电冰箱\"},{\"three\":\"空调\"},{\"four\":\"洗衣机\"}]}]";
在前端用json方式把内容提取出来
你的json串最外面多了两个大括号吧,在前端提取的话是想用js还是嵌套java代码,js的话用eval()或者JSON.parse()
你这json 格式有问题啊!
var jsonStr = "[{\"大家电\":[{\"one\":\"电视机\"},{\"two\":\"电冰箱\"},{\"three\":\"空调\"},{\"four\":\"洗衣机\"}]}]";
var json = eval('(' + jsonStr + ')');
for (var i = 0; i < json[0].大家电.length; i++) {
for (attr in json[0].大家电[i]) alert(json[0].大家电[i][attr])
}
/* 获取天气 */
function getWeather(){
var ipUrl = "${base}/weather/getWeatherByaddr.jhtml";
$.ajax({
url: ipUrl,
async: true,
type: "get",
dataType: "json",
success: function(data) {
var weather = data.retData.today.type;
var temperature = data.retData.today.curTemp;
}
});
}
$(function(){
getWeather();
});
/* 获取天气结束 */
JSON返回示例 :
{
errNum: 0,
errMsg: "success",
retData: {
city: "北京",
cityid: "101010100",
today: {
date: "2015-08-03",
week: "星期一",
curTemp: "28℃",
aqi: "92",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "30℃",
lowtemp: "23℃",
type: "阵雨",
index: [
{
name: "感冒指数",
code: "gm",
index: "",
details: "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。",
otherName: ""
},
{
code: "fs",
details: "属中等强度紫外辐射天气,外出时应注意防护,建议涂擦SPF指数高于15,PA+的防晒护肤品。",
index: "中等",
name: "防晒指数",
otherName: ""
},
{
code: "ct",
details: "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。",
index: "炎热",
name: "穿衣指数",
otherName: ""
},
{
code: "yd",
details: "有降水,推荐您在室内进行低强度运动;若坚持户外运动,须注意选择避雨防滑并携带雨具。",
index: "较不宜",
name: "运动指数",
otherName: ""
},
{
code: "xc",
details: "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。",
index: "不宜",
name: "洗车指数",
otherName: ""
},
{
code: "ls",
details: "有降水,不适宜晾晒。若需要晾晒,请在室内准备出充足的空间。",
index: "不宜",
name: "晾晒指数",
otherName: ""
}
]
},
forecast: [
{
date: "2015-08-04",
week: "星期二",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "32℃",
lowtemp: "23℃",
type: "多云"
},
{
date: "2015-08-05",
week: "星期三",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "30℃",
lowtemp: "23℃",
type: "多云"
},
{
date: "2015-08-06",
week: "星期四",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "29℃",
lowtemp: "24℃",
type: "雷阵雨"
},
{
date: "2015-08-07",
week: "星期五",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "30℃",
lowtemp: "24℃",
type: "多云"
}
],
history: [
{
date: "2015-07-31",
week: "星期五",
aqi: "52",
fengxiang: "无持续风向",
fengli: "微风级",
hightemp: "高温 29℃",
lowtemp: "低温 22℃",
type: "多云"
},
{
date: "2015-08-01",
week: "星期六",
aqi: null,
fengxiang: "南风",
fengli: "微风级",
hightemp: "高温 35℃",
lowtemp: "低温 26℃",
type: "多云"
}
]
}
}
你可以根据这个把你的json解析出来