JS解析JSON字符串
varresult={"Category":[{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/y...
var result={"Category":[{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/yinpin.jpg"},{"categoryId":2,"categoryName":"食品","categoryImage":"/upload/shiping.jpg"},{"categoryId":3,"categoryName":"酒类","categoryImage":"/upload/jiullei.jpg"}],
"recommend":{"id":11,"productName":"统一
老坛泡椒牛肉袋面香辣味
110g*24袋","filenameSmall":"/upload/ty_ltpj_small.jpg","productPrice":48.0,"productCost":47.5}}
var json=JSON.parse(result);
alert(json.Category.categoryName); 这个弹出框显示该值未定义
为什么?应该怎么取到这个值 展开
"recommend":{"id":11,"productName":"统一
老坛泡椒牛肉袋面香辣味
110g*24袋","filenameSmall":"/upload/ty_ltpj_small.jpg","productPrice":48.0,"productCost":47.5}}
var json=JSON.parse(result);
alert(json.Category.categoryName); 这个弹出框显示该值未定义
为什么?应该怎么取到这个值 展开
展开全部
var result={"Category":[{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/yinpin.jpg"},{"categoryId":2,"categoryName":"食品","categoryImage":"/upload/shiping.jpg"},{"categoryId":3,"categoryName":"酒类","categoryImage":"/upload/jiullei.jpg"}],"recommend":{"id":11,"productName":"统一老坛泡椒牛肉袋面香辣味110g*24袋","filenameSmall":"/upload/ty_ltpj_small.jpg","productPrice":48.0,"productCost":47.5}};
var val = result.Category[0].categoryId;
这样就能得到值了啊
你的代码失效的原因有两个:
1、result已经是JSON格式的数据了,所以不需要再次用JSON.parse(result)进行转换
2、result.Category是个数组,所以要用[0]来取出某一项的值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询