jquery 2维JSON

时间:2012-03-14 18:05:30

标签: jquery json

以下是有效的,如果我有这样的话:

  return Json(new { CustomerInfo = custinfo}); // defined in the controller

  // below I define in my client script
  jQuery.each(CustomerInfo, function () {
                 jQuery.each(this, function () {
                     // get field info from the object
                 });
             });

但是,如果我这样传回2个数据

     return Json(new { CustomerInfo = custinfo, Message = msg });

请注意,custinfo是一个列表,而消息是一个字符串

在我的.ajax()中,我有以下内容从JSON中检索信息

     function (data) {
        alert(data.Message); // show up fine

        alert(JSON.stringify(data.RepInfo));

       jQuery.each(data.CustomorInfo, function () {
          jQuery.each(this, function () {
              // get data for each field . Show up as undefined here for my row content
          });
       });

     } 

如果我在上面的代码中对JSON.stringify发出警报,我会得到以下内容:

[{"ID":"12","Date":"01/23/2012","City":"Clearwater","State":"FL"},{"ID":"00017-LV01-12","Date":"02/09/2012","City":"Peoria","State":"IL"},{"ID":"00010-LV01-12","Date":"06/22/2012","City":"Newport Beach","State":"CA"}]

当我查看.each()的数据时,它正在为行内容提取未定义的内容。 如何让它显示行的内容。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

       $.each(data.CustomorInfo,function(i) {

           $.each(this, function(key, value) {
                        alert(key +'='+ value);  
                });
           });

        });

现场演示:http://jsfiddle.net/PQcFx/18/

答案 1 :(得分:0)

您的代码似乎在我的测试中正常工作。

http://jsfiddle.net/TapfF/2/

不知道如何在服务器/后端定义“custinfo”...

$(document).ready(function() {
    var data = {
        "CustomerInfo": [{"ID":"12","Date":"01/23/2012","City":"Clearwater","State":"FL"},{"ID":"00017-LV01-12","Date":"02/09/2012","City":"Peoria","State":"IL"},{"ID":"00010-LV01-12","Date":"06/22/2012","City":"Newport Beach","State":"CA"}],
        "Message": "testing123"
    };

    alert(data.Message);
    jQuery.each(data.CustomerInfo, function() {
        alert("outer: " + this);
        jQuery.each(this, function() {
            alert("inner: " + this);
        });
    });
});