返回不读取的JSON对象

时间:2018-07-06 13:21:41

标签: c# json ajax

我想显示我的数据及其中的行数。 我正在返回json,并且那里存在一些错误

    public JsonResult NewData()
    {
        List<mydatasample> mydata = new List<mydatasample>(); 
        mydata.Add(new mydatasample { bookName = " Book 1 ", publisherName = " Name1 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 2 ", publisherName = " Name2 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 3 ", publisherName = " Name3 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 4 ", publisherName = " Name4 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 5 ", publisherName = " Name5 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 6 ", publisherName = " Name6 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 7 ", publisherName = " Name7 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 8 ", publisherName = " Name8 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 9 ", publisherName = " Name9 ", publishYear = 2018 });
        mydata.Add(new mydatasample { bookName = " Book 10 ", publisherName = " Name10 ", publishYear = 2018 });
        int rowcount= mydata.Count;

        return Json(new { mydata = mydata,rowcount = rowcount},  JsonRequestBehavior.AllowGet);
    }

这是我的ajax代码。

错误即将到来

  

e [j] .bookName,e [j] .publisherName,e [j] .publishYear

具体来说,我的错误是

  

SomeActionMethod:59未捕获的TypeError:无法读取属性   未定义的“ bookName”        在Object.success处(SomeActionMethod:59)        着火了(jquery-1.10.2.js:3062)        在Object.fireWith [as resolveWith](jquery-1.10.2.js:3174)        完成时(jquery-1.10.2.js:8249)        在XMLHttpRequest.callback(jquery-1.10.2.js:8792)

        $.ajax(
        {
            cache: false,
            type: "GET",
            url: hosturl,
            success: function (e)
            {

                var var1 = "<table>";
                var1 += "<tr> <th> Book Name </th> <th> Publisher Name </th> <th> Publish Year </th> </tr>";
                for (var j = 0; j < 10; j++)
                {
                    //error is coming in this line
                    var1 += "<tr>" + "<td>" + e[j].bookName + "</td>" + "<td>" + e[j].publisherName + "</td>" + "<td>" + e[j].publishYear + "</td>" + "</tr>";
                }
                var1 += "</table>";
                $("#t1").append(var1);

                var rowCount = $("#t1").find.length;
                for(var j = 1; j<e.rowcount; j++)
                {
                     var rowCount = $("#t1").find.length;
                     rowCount++;
                }
                var1 += "</table>";
                $("#t1").append(rowCount);
            },

2 个答案:

答案 0 :(得分:0)

您可以像这样创建一个json对象:

{  
    "mydata":[  
       {  
          "bookName":" Book 1 ",
          "publisherName":" Name1 ",
          "publishYear":2018
       },
       {  
          "bookName":" Book 2 ",
          "publisherName":" Name2 ",
          "publishYear":2018
       },
      ...
    ],
    "rowcount":10
}

您必须修改循环:

for (var j = 0; j < e.rowcount; j++) {
    var1 += "<tr>" + "<td>" + e.mydata[j].bookName + "</td>" + "<td>" + e.mydata[j].publisherName + "</td>" + "<td>" + e.mydata[j].publishYear + "</td>" + "</tr>";
}

答案 1 :(得分:-1)

您首先需要将返回的对象转换为javascript对象,因为返回的默认对象将是JSON字符串值,而不是您可以通过[]访问的对象。

请考虑以下代码段:

$.ajax(
        {
            cache: false,
            type: "GET",
            url: hosturl,
            success: function (e)
            {
              var data = JSON.parse(e);
              // now you can call something like this
              console.log(data[0].bookName);
            }
        });

现在您有了一个对象,可以按照原始问题进行循环并从中提取信息。