代码之家  ›  专栏  ›  技术社区  ›  user70192

用jquery读取json

  •  1
  • user70192  · 技术社区  · 14 年前

    我使用jquery在web服务中执行操作。在将数据写回我的databases之后,服务返回一个json响应。我的请求如下:

    $.ajax({
      url: "/services/myService.svc/PostMessage",
      type: "POST",
      contentType: "application/json; charset=utf-8",
      data: '{"message":"testing","comments":"test"}',
      dataType: "json",
      success: function (response) {
        if ((response.d != null) && (response.d.length > 0)) {
           // Parse the status code here
        }
        else { alert("error!"); }
      },
      error: function (req, msg, obj) {
        alert("error: " + req.responseText);
      }
    });
    

    当返回我的响应时,response.d包含以下内容:

    [{“状态码”:1}]

    如何解析statuscode的值?

    2 回复  |  直到 14 年前
        1
  •  3
  •   SLaks    14 年前

    这是一个包含 StatusCode 财产。

    你可以写

    alert(response.d[0].StatusCode)
    
        2
  •  0
  •   Buhake Sindi Tesnep    14 年前

    如果函数返回 d 可以执行以下操作的对象:

    if ((response.d != null) && (response.d.length > 0)) {
           // Parse the status code here
           for (var i = 0; i < response.d.length; i++) {
              alert(response.d[i].StatusCode);
            }
        }
    

    希望这有帮助。