【问题标题】:Unable to parse JSON string returned from Server Side method in ASP.Net无法解析从 ASP.Net 中的服务器端方法返回的 JSON 字符串
【发布时间】:2014-08-28 18:19:52
【问题描述】:

我正在尝试解析这个 json 数据

[
    {   "title":"Yorkshire 199/8 * v Durham 237/10", 
        "link":"http://www.cricinfo.com/ci/engine/match/693421.html?CMP=OTC-RSS", 
        "description":"Yorkshire 199/8 * v Durham 237/10", 
        "guid":"http://www.cricinfo.com/ci/engine/match/693421.html"},
    {
        "title":"Essex v Warwickshire 271/7 *", 
        "link":"http://www.cricinfo.com/ci/engine/match/693423.html?CMP=OTC-RSS", 
        "description":"Essex v Warwickshire 271/7 *", 
        "guid":"http://www.cricinfo.com/ci/engine/match/693423.html"},
    {
        "title":"Singapore v Malaysia", 
        "link":"http://www.cricinfo.com/ci/engine/match/774365.html?CMP=OTC-RSS", 
        "description":"Singapore v Malaysia", 
        "guid":"http://www.cricinfo.com/ci/engine/match/774365.html"}
]

从服务器端方法返回并使用该方法遍历每个项目

$.ajax({
               type: "POST",
               url: "Default.aspx/ServerSideMethod",
               //data: JSON.stringify({ 'p': 'Sent Text' }),
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               async: true,
               cache: false,
               success: function (data) {
                   //alert(data.d) //returns the result set displayed above.
                   $.each(data, function (i, obj) {
                       alert(obj.title); // returns undefined.

                   });
               },
               error: function (x, e) { alert(x.responseText); }
           })

但它总是返回未定义的。 我的 jquery 函数或数据有什么问题吗?我的服务器端函数工作正常,上面的 Jquery 函数也显示返回值,但我无法解析这个值。

我也试过这个thread 来解决这个问题,但没有成功。 请任何人...

【问题讨论】:

  • console.log(data); 是你所期望的吗?
  • 我需要在我的网页上显示所有三个项目。这就是为什么我要获取obj.title、obj.description等等...
  • @liaqatali 没关系。 console.log(data) 向您展示了什么?
  • 所以data 不是您在上面发布的内容。解决这个问题,看看你会得到什么。
  • 那么这就是你需要迭代的:$.each(data.d, ...

标签: javascript jquery asp.net json


【解决方案1】:

您正在迭代错误的对象:

$.ajax({
           type: "POST",
           url: "Default.aspx/ServerSideMethod",
           //data: JSON.stringify({ 'p': 'Sent Text' }),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           async: true,
           cache: false,
           success: function (data) {
               //alert(data.d) //returns the result set displayed above.
               $.each(data.d, function (i, obj) { // <---- use data.d here, not data
                   alert(obj.title);

               });
           },
           error: function (x, e) { alert(x.responseText); }
       })

当您只是迭代 data 时,obj 会获取数据的每个属性的值。因此,例如,在某些时候,objdata.d。然后你尝试提醒obj.title,它是data.d.title,并不存在。

通过迭代data.d,您将obj 设置为例如data.d[0]。那么,

obj.title === data.d[0].title
          === "Yorkshire 199/8 * v Durham 237/10"`

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多