json响应中的字符无效

时间:2015-06-20 07:51:39

标签: javascript php jquery ajax json

我无法处理json响应,在访问json响应时我在json响应中得到一些无效字符,那么我们如何使用php scirpt从json响应中删除空格问题和无效字符

我的json回复:

[{"keyword":"cosmetic dermatology","svol":4400},{"keyword":"dermatology associates","svol":22200},{"keyword"
:"advanced dermatology","svol":40500},{"keyword":"dermatology clinic","svol":3600},{"keyword":"dermatology
 specialists","svol":3600},{"keyword":"dermatology consultants","svol":5400},{"keyword":"pediatric dermatology"
,"svol":3600},{"keyword":"what is dermatology","svol":1900},{"keyword":"dermatology pictures","svol"
:1300},{"keyword":"dermatological","svol":2400},{"keyword":"laser dermatology","svol":1300},{"keyword"
:"dermatology group","svol":1900},{"keyword":"dermatology uk","svol":390},{"keyword":"dermatology courses"
,"svol":1000},{"keyword":"dermatologic","svol":1600},{"keyword":"westlake dermatology","svol":8100},
{"keyword":"pariser dermatology","svol":3600},{"keyword":"aesthetic dermatology","svol":1000},{"keyword"
:"dermatology doctors","svol":590},{"keyword":"north dallas dermatology","svol":1300} ]

我的JQuery是:

$.ajax({
            type:"post", 
            datatype : 'json',
            url:"GetKeyWordBids.php", 
            data:"specialty="+ spevalue,
            success: function(s) {
                var object = $.parseJSON(s);
                /*oTable.fnClearTable();
                oTable.fnAddData([
                    s
                ]); */
            }
        }); 

FireBug输出:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 25230 of the JSON data

Jsonlint.com输出json响应:

Parse error on line 75:
...        "keyword": "ear nose and throat
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

2 个答案:

答案 0 :(得分:1)

由于指定了$.parseJSON();,因此您无需使用datatype : 'json'。返回的数据已经是JSON格式,只需直接使用即可。

答案 1 :(得分:1)

请将您的JSON数据粘贴到此处: http://jsonlint.com/ 并且看到JSON语法不好。

当你解决这个问题时,请使用Satpal答案(不包括$ .parseJSON)