使用JQuery解析JSON

使用JQuery解析JSON,json,Json,我正在从我的WCF服务接收JSON字符串响应。我想把这个JSON解析成相应的对象。所以我做了如下的事情 $.ajax({ type: 'GET', url: 'http://URL/Service.svc/LoginValidation?', success: function(response, status, xhr) { if (response != "") { var JSON=response.replace(/^"|"$/g, '\'

我正在从我的WCF服务接收JSON字符串响应。我想把这个JSON解析成相应的对象。所以我做了如下的事情

$.ajax({
    type: 'GET',
    url: 'http://URL/Service.svc/LoginValidation?',
    success: function(response, status, xhr) {
    if (response != "") {
        var JSON=response.replace(/^"|"$/g, '\''); // replace Start and End double Quotes with single quotes. becze JSON string should be start and end with single quotes while parsing this.
        var obj = JSON.parse(JSON); // Here is my problem. While accessing JSON variable here that automatically showing double quotes. so that here showing syntax error.
            UserID = obj.UserID;
            ClientID = obj.ClientID;
            DomainName = obj.DomainName;
            AuthenticationKey = obj.AuthenticationKey;
        }
        else {
            alert("Invalid UserName or Password.");
        }
    }
});

如何解析这个JSON数据。我们可以使用JQuery来实现这一点。

只需在$.ajax调用选项中设置数据类型:“json”,JQuery将解析它,并将解码的对象提供给成功处理程序。

使用getJSON函数作为

      $.getJSON('http://URL/Service.svc/LoginValidation?',function(data)
       {
        var JSON=data.replace(/^"|"$/g, '\''); 
        UserID = JSON.UserID;
        ClientID = JSON.ClientID;
        DomainName = JSON.DomainName;
        AuthenticationKey = JSON.AuthenticationKey;
           // do remaining stuffs
       })  
    });

将$.ajax调用的数据类型设置为json,或者只使用方法,因为您没有使用错误回调。如果您在ajax请求中通过dataType:json属性指定返回的数据类型,jquery会自动执行此操作。非常好。工作很好。。非常感谢大家。你伟大的兰兹。。谢谢: