日期:2014-05-16  浏览次数:20439 次

怎么使用jQuery解析这段json类型的数据
[{"objName":"datalist","objUser":[{"sUserName":"JSON0","sAddress":"中国-北京-海淀0号"},{"sUserName":"JSON1","sAddress":"中国-北京-海淀1号"},{"sUserName":"JSON2","sAddress":"中国-北京-海淀2号"},{"sUserName":"JSON3","sAddress":"中国-北京-海淀3号"},{"sUserName":"JSON4","sAddress":"中国-北京-海淀4号"}]}]


这是我写的。搞不定啊.
$(function() {
var json = [{"objName":"datalist","objUser":[{"sUserName":"JSON0","sAddress":"中国-北京-海淀0号"},{"sUserName":"JSON1","sAddress":"中国-北京-海淀1号"},{"sUserName":"JSON2","sAddress":"中国-北京-海淀2号"},{"sUserName":"JSON3","sAddress":"中国-北京-海淀3号"},{"sUserName":"JSON4","sAddress":"中国-北京-海淀4号"}]}]
    var data = eval('(' + json + ')');
    alert(data.objName);
    $.each(data.objUser,
    function(i, o) {
        alert(o.sAddress);
    });
})



我要弹出提示:datalist,然后是每个sAddress的值


求高人指点

------解决方案--------------------

var json = [{"objName":"datalist","objUser":[{"sUserName":"JSON0","sAddress":"中国-北京-海淀0号"},{"sUserName":"JSON1","sAddress":"中国-北京-海淀1号"},{"sUserName":"JSON2","sAddress":"中国-北京-海淀2号"},{"sUserName":"JSON3","sAddress":"中国-北京-海淀3号"},{"sUserName":"JSON4","sAddress":"中国-北京-海淀4号"}]}];
for(i in json){
     alert(json[i].objName);
     for(s in json[i].objUser){
        alert(json[i].objUser[s].sAddress);
       }
}

------解决方案--------------------
json变量本来就是JSON格式的了,LZ为什么还要用var data = eval('(' + json + ')');把这红色这句去了。


$(function() {
    var json = [
                  {
                      "objName":"datalist",
                      "objUser":[
                                  {"sUserName":"JSON0","sAddress":"中国-北京-海淀0号"},
                                  {"sUserName":"JSON1","sAddress":"中国-北京-海淀1号"},
                                  {"sUserName":"JSON2","sAddress":"中国-北京-海淀2号"},
                                  {"sUserName":"JSON3","sAddress":"中国-北京-海淀3号"},
                                  {"sUserName":"JSON4","sAddress":"中国-北京-海淀4号"}