我从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做到这一点。
答案 0 :(得分:0)
只需在dataType: 'json'
调用选项中设置$.ajax()
,jQuery就会对其进行解析,并将解码后的对象提供给success
处理程序。
答案 1 :(得分:0)
将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
})
});