使用Jquery解析JSON

tec*_*iac 2 ajax jquery json

我有一个页面可以快速登录检查并访问一些Web服务以获取一些JSON数据.它返回的数据如下所示:

{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } 
Run Code Online (Sandbox Code Playgroud)

我用来获取这些数据并尝试根据信息弹出模态窗口的代码是:

   $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Login.asmx/Logon",
            data: strData,
            dataType: "json",
            success: function (response) {
                $('#divResp').html(response.d);
                $(response.d).each(function(index,item){
                    alert(item.campaignid);
                });
                if (res.indexOf('invalid') >= 0){                       
                    //invalid account information UNP                       
                    foo = "You entered an invalid account number or password";
                    showDialog('screenActivate.asp',test_imgName);
                } 
                if (res.indexOf('none') >=0) {
                    //do not deliver splash screen
                    foo = "No splash screen for YOU!";
                } 
                if (res.indexOf('error') >=0) {
                    //general error
                    foo = "You entered an error";
                }
                if (res.indexOf('frozen') >=0) {
                    //member is locked out from numberous failed attempts
                    foo = "You have too many failed attempts.  Your account has been locked";
                }
                if (res.indexOf('.png') >=0) {
                    imgName = (response.d);
                    showDialog('screenActivate.asp',test_imgName);
                }
                alert(foo);
                $('#divResp').html(response.d);
            }
        });
        $('#divQuery').ajaxError(function(evt, request, settings){
            $('#divResp').html("Web services timeout.  Please try again.");             
        });
    }
Run Code Online (Sandbox Code Playgroud)

挑战是,要么我没有得到任何信息(未定义),要么得到当前迭代,来自Firebug的错误表明未捕获的异常:语法错误,无法识别的表达式:'INVALID'

我没有正确解析JSON响应吗?

相关评论回答

我粘贴:"d":"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } "进入jsonlint.com,该工具说JSON是有效的.当我尝试response.d使用每个语句循环时,它似乎逐个字符.

jon*_*ohn 6

JSON规范要求使用双引号(")而不是单引号(')

官方JSON字符串解析图(来自http://www.json.org)

http://www.json.org/string.gif

回应你的评论......

好的,这是交易......

"d":"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } "
Run Code Online (Sandbox Code Playgroud)

是有效的JSON ......但它只是一个d包含字符串属性的对象...

"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } "
Run Code Online (Sandbox Code Playgroud)

以你期望的方式工作的数据看起来像这样......

{"d": {"RESPONSE":"INVALID","CAMPAIGNID":"0","MORELINK":""}}
Run Code Online (Sandbox Code Playgroud)