日期:2014-05-17  浏览次数:20528 次

【加急!!】json转成数组,求高手指教,在线等...
aspx中有一个json对象msg,代码如下:
$.ajax({
  type: "POST",
  url: "UserExists.ashx",
  //用text接收
  dataType: "text",
  data: "userId=loadTiemTree",
  success: function (msg) {
  var $form = $("form");
  if (msg != null && msg != "") {
  //把msg转换成数组
   
  //显示后台数据
  $form.append("<div style=\"width:1000px;margin:0px auto;\"><div style=\"width:495px;float:left;border:1px outset red;\">"
  + msg + "</div></div>");
  $("#page_tag_load").hide();

  } else {
  $("#page_tag_load").hide();
  }
  }
  });

输出后的结果如下:
[{"newid":"673"},{"newimg":"~/Sources/Image/treetime/20121010205839534.jpg"},{"newcontent":""}",{"newsclick":"0"},{"newtime":"2012/10/10 20:58:36"}][{"newid":"672"},{"newimg":"~/Sources/Image/treetime/20121010205844616.jpg"},{"newcontent":""}",{"newsclick":"0"},{"newtime":"2012/10/10 20:58:19"}][{"newid":"671"},{"newimg":"~/Sources/Image/treetime/20121010205713751.jpg"},{"newcontent":""}",{"newsclick":"0"},{"newtime":"2012/10/10 20:57:53"}][{"newid":"670"},{"newimg":"~/Sources/Image/treetime/20121010205758428.png"},{"newcontent":""}",{"newsclick":"0"},{"newtime":"2012/10/10 20:57:31"}]
我想在页面上把它转成数组,然后获取数组里面的信息赋给控件,该怎么做啊,求高手指教,在线等。


------解决方案--------------------
一般是使用
eval(msg)

你的数据不规范

{"newcontent":""}",
这里有多余的"


正确格式可以这样
JScript code
<script type="text/javascript">
  msg = '[{"newid":"673"},{"newimg":"~/Sources/Image/treetime/20121010205839534.jpg"},{"newcontent":""},{"newsclick":"0"},{"newtime":"2012/10/10 20:58:36"}][{"newid":"672"},{"newimg":"~/Sources/Image/treetime/20121010205844616.jpg"},{"newcontent":""},{"newsclick":"0"},{"newtime":"2012/10/10 20:58:19"}][{"newid":"671"},{"newimg":"~/Sources/Image/treetime/20121010205713751.jpg"},{"newcontent":""},{"newsclick":"0"},{"newtime":"2012/10/10 20:57:53"}][{"newid":"670"},{"newimg":"~/Sources/Image/treetime/20121010205758428.png"},{"newcontent":""},{"newsclick":"0"},{"newtime":"2012/10/10 20:57:31"}]'.replace(/\]\[/g, "],[");
  arr = eval('new Array(' + msg + ')');
  for (i = 0; i < arr.length; i++) {
    alert(arr[i][0].newid);
    alert(arr[i][1].newimg);
  }
</script>