JS解析JSON字符串

问题描述:后台需要传递给前台一些数据,用于页面数据显示,因为是一些Lable标签,所以数据传递到前台需要解析。

思路:因为数据比较杂乱,所以我选择传递的数据类型是Json格式,但是数据展示时需要解析成单个的字符串,赋值给Lable标签

一、JSON字符串解析

Json格式字符串(键/值)

{"lblTime":"2016-11-04 14:15:26","lblArea":"B包头地区(新)","lblTelevisionLogStoreName":"123","lblRecordCount":"94"}

 

主要用函数eval对json格式字符串进行反序列化操作,然后获取节点对应的值

  var obj = eval("(" + result + ")");//eval函数对json格式字符串进行反序列化
  document.getElementById("lblTime").innerText = obj["lblTime"];//obj["lblTime"]获取值
  document.getElementById("lblArea").innerText = obj["lblArea"];
  document.getElementById("lblTelevisionLogStoreName").innerText = obj["lblTelevisionLogStoreName"];
  document.getElementById("lblRecordCount").innerText = obj["lblRecordCount"];

二、JSON字符串解析

Json格式字符串(键/[]),这种是一个键对应一个数组

{"Category":[{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/yinpin.jpg"},{"categoryId":2,"categoryName":"食品","categoryImage":"/upload/shiping.jpg"},{"categoryId":3,"categoryName":"酒类","categoryImage":"/upload/jiullei.jpg"}]}

数据取值:

var result = eval("(" + result + ")");
result.Category[0].categoryId; result.Category[0].categoryName; result.Category[0].categoryImage; result.Category[1].categoryId; result.Category[1].categoryName; result.Category[1].categoryImage; result.Category[2].categoryId; result.Category[2].categoryName; result.Category[2].categoryImage;

 

posted @ 2016-11-04 14:46  高空燕子飞过  阅读(773)  评论(0编辑  收藏  举报