简体   繁体   English

如何在jQuery中导航JSON对象?

[英]How to navigate JSON Object in jQuery?

I made this webservice that handles my database functions, and this is an AJAX call to one of the methods. 我做了这个处理数据库功能的Web服务,这是对一种方法的AJAX调用。

 $.ajax({
        type: "POST",
        url: "Service/dataBaseService.asmx/getRMAData",
        data: '{"RMAId": 1 }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (data) {
            console.log(data);
            alert(data.RMA_ID);
        }
  });

this is what is logged: 这是记录的内容:

({d:"[{\"RMA_ID\":1,\"RequestDate\":\"2013-02-28T00:00:00\",\"Company\":1,\"Status\":\"Accepted            \",\"Serial\":201764,\"LastChangeDate\":\"2013-02-28T00:00:00\",\"LastChangeBy\":\"Foreign    \",\"Price\":null}]"})

However alert(data.RMA_ID) returns undefined aswell as data.d.RMA_ID ? 但是alert(data.RMA_ID)返回未定义的数据以及data.d.RMA_ID吗?

How can I get hold of the values? 如何掌握这些价值?

The value of data that you've logged is an object with a property named d , that contains a string value. 您记录的data值是一个对象,其属性名为d ,其中包含字符串值。 You could probably make adjustments at your server side to make the value of d an object rather than a string, but the way it is currently constructed, you would be able to parse it into an object using JSON.parse . 您可能在服务器端进行了调整,以使d的值成为对象而不是字符串,但是当前构造它的方式是,可以使用JSON.parse将其解析为对象。

Once you've done that, the resulting object should be an array, containing one single object. 完成此操作后,结果对象应该是一个数组,其中包含一个对象。 Thus, your access to RMA_ID would be as follows: 因此,您对RMA_ID的访问将如下所示:

var data = JSON.parse(data.d);
alert(data[0].RMA_ID);

Using simple javascript you need to parse JSON response 使用简单的JavaScript,您需要解析JSON响应

var resp = eval('(' + data + ')');

or thru jQuery 或通过jQuery

var resp = jQuery.parseJSON(data);

now you can access the data using '.' 现在您可以使用“。”访问数据。 and key name 和键名

console.log(resp.d[0].RMA_ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM