JSON.parse:意外字符

4

我有两个来自PHP代码的JSON文件,我认为它们是相同的。

1. 第一个JSON文件

 {"setmargin":["20","20","20","20","20","20","20","20","20","20","20","20","20","20","20","20","20","20","20","20"],"set_code":["WS-341C","WS-3779","WS-3D0E","WS-4D3F","WS-34CB","WS-954A","WS-1B22","WS-640F","WS-F30A","WS-927E","WS-1264","WS-E0DF","WS-423E","WS-0CFE","WS-93DC","WS-EFFF","WS-8048","WS-7309","WS-A24C","WS-42D0"],"post":["Not yet","not yet","not yet","not yet","not yet","not yet","Belum","not yet","not yet","not yet","not yet","not yet","not yet","Belum","not yet","not yet","not yet","not yet","not yet","Done"],"image":["https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/11\/14\/1086033\/1086033_093180f9-1fb5-416f-93ea-63e6ad7a21be.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/23\/13883540\/13883540_d64b757a-b059-416b-b92c-f213241ff451.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/9\/22\/181965\/181965_cc46d42c-605e-4ab2-83eb-3696d73dba28.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/9\/22\/181965\/181965_392e6d5a-47f2-4466-9f17-d3b150128cb1.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/24\/27103490\/27103490_42f9cb14-b8e4-4912-8f51-686b281922d7.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/24\/27103483\/27103483_abf725e4-bc38-4718-9503-07ca8dfa5684.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/24\/27103469\/27103469_b01e5794-f212-4961-bdd5-e4a991ab9564.jpg","https:\/\/ecs7.site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_3a80c9aa-6f54-4da9-9c03-60761aaafbec.jpg","https:\/\/ecs7.site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_b23028cd-ddd4-4df3-b2ae-8ba61d1aa2c4.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_e70a6912-9ff4-4f98-8636-186ddbd36d0c.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_7e32ee78-265e-4a16-8afc-9e96279f1e17.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_c9e71217-de50-4569-b7be-be6f0b497248.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_2df1a94f-c020-46a6-b913-2eb8fc192fff.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_4dc460aa-65b4-474a-8c56-b4c8cddc9085.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_0e2a3174-9e65-4ffe-aa22-46ccd80bde44.jpg","https:\/\/ecs7.site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_b5429498-37a8-4ee6-89be-483f05be6c21.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_415bc370-cda4-4ae2-8493-9d2906c60f42.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_63c2fa8c-00e5-441a-a513-246b0a1b8b11.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_0f912de5-61f1-4e67-9a3e-7e92c783b148.jpg","https:\/\/site.net\/img\/cache\/200-square\/product-1\/2015\/12\/21\/1086033\/1086033_5bafccf1-929d-4151-8a2b-5cd3f885ba8d.jpg"],"price_marg":[30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30],"productlink":["https:\/\/www.site.com\/swiss-army-ba405l-date-leather-dark-brown-list-white","https:\/\/www.site.com\/luminor-panerai-02-leather","https:\/\/www.site.com\/rolex-daytona-full-black","https:\/\/www.site.com\/rolex-daytona-black-gold","https:\/\/www.site.com\/swiss-army-dual-time-101-silver-white","https:\/\/www.site.com\/swiss-army-dual-time-101-silver-black","https:\/\/www.site.com\/swiss-army-dual-time-101-full-black","https:\/\/www.site.com\/naviforce-chrono-original-leather-brown","https:\/\/www.site.com\/naviforce-chrono-original-leather-red","https:\/\/www.site.com\/naviforce-chrono-original-leather-black","https:\/\/www.site.com\/reddington-number-original-leather-green","https:\/\/www.site.com\/reddington-number-original-leather-blacksilver","https:\/\/www.site.com\/reddington-number-original-leather-full-black","https:\/\/www.site.com\/reddington-number-original-leather-brownsilver","https:\/\/www.site.com\/reddington-number-original-leather-brown-black","https:\/\/www.site.com\/reddington-number-original-leather-cream","https:\/\/www.site.com\/quiksilver-date-dimension-silver-white","https:\/\/www.site.com\/quiksilver-date-dimension-full-black","https:\/\/www.site.com\/dkny-ladies-gold-white","https:\/\/www.site.com\/dkny-ladies-rosegold-black"],"detail":["Jam Tangan Pria Swiss Army BA405L DATE Leather Dar","Luminor Panerai 02 Leather","Jam Tangan Pria \/ Cowo Rolex Daytona Rantai Full B","Jam Tangan Pria \/ Cowo Rolex Daytona Rantai Black","Jam Tangan Pria \/ Cowok Swiss Army Dual Time 101 R","Jam Tangan Pria \/ Cowok Swiss Army Dual Time 101 R","Jam Tangan Pria \/ Cowok Swiss Army Dual Time 101 R","Jam Tangan Pria \/ Cowok Naviforce Chrono Detik Ori","Jam Tangan Pria \/ Cowok Naviforce Chrono Detik Ori","Jam Tangan Pria \/ Cowok Naviforce Chrono Detik Ori","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Reddington Number Original","Jam Tangan Pria \/ Cowok Quiksilver Date Dimension","Jam Tangan Pria \/ Cowok Quiksilver Date Dimension","Jam Tangan Wanita \/ Cewek DKNY Ladies Rantai Gold","Jam Tangan Wanita \/ Cewek DKNY Ladies Rantai Roseg"],"price":["95000","187000","380000","360000","155000","155000","155000","190000","190000","190000","185000","185000","185000","185000","185000","185000","125000","125000","160000","160000"],"page":["https:\/\/www.site.com\/page\/2"]}

2.json 2

 {"setmargin":["20","20"],"set_code":["TR-2C2C","TR-1086"],"post":["NOte yet","Not yet"],"image":["www.site.com\/item_show.php?id=156801&flag=s1","www.site.com\/item_show.php?id=156800&flag=s1"],"hargajual":[125000,125000],"productlink":["www.site.com\/indexphp?hal=detail&id=156801&show=COUPLE&nama=COUPLE ROLEX SILVER","www.site.com\/indexphp?hal=detail&id=156800&show=COUPLE&nama=ROLEX SILVER"],"detail":["JC53","JC50"],"price":["105","105"],"page":[]}

Json 1 在javascript中使用JSON.parse解析时没有问题。 但是 Json 2 出现了以下错误:

"SyntaxError: JSON.parse: 在JSON数据的第1行第2列处出现意外字符"

我漏掉了什么吗?

用于创建JSON的PHP代码:

$arr = array("setmargin"=>$setmargin,"setkodebarang"=>$setkodebarang,"fbpost"=>$fbpost,"image"=>$imagesurl,"hargajual"=>$hargajual,"productlink"=>$produckurl,"detail"=>$detail,"price"=>$priceprod,"page"=>$halamanSup);
echo json_encode(  $arr );

Jquery处理JSON:

$.ajax({
            type        : "POST",
            url         : "processaction.php",
            data        : {
                            action_type     : "loaddefaultcontent",
                            supllier_id     : $( "button#sup" ).first().attr('sp_id'),
                            item_slug       : $( "#navbar" ).find("li.active a").attr("class"),
                            template        : gettemplate
            },
            beforeSend  : function(){
                $("#productList").html('<div class="loader"><img class="img-responsive center-block" src="images/load.gif"/></div>');
            },
            success     : function(dapet){

                if(dapet){
                    console.log(dapet);
                    var data = JSON.parse(dapet);
                    console.log(data);
                    $( "button#sup" ).first().addClass('btn-success active');
                    $('.loader').hide();
                    for(var i=0; i < data.setkodebarang.length; i++){
                        setContent(data,i);
                    }

                    if( data.page.length == 1 ){
                        $('.next>button').attr("data_href",data.page[0]);
                        $('.next').show();
                        //$('.previous>button').hide();
                    }//else{
                    //  $('.next>button').attr("data_href",data.page[0]);
                    //  $('.next>button').show();
                    //  //$('.previous>button').attr("data_href",data.page[1]);
                    //}

                }

            },
        error       : function(XMLHttpRequest, textStatus, errorThrown) {
                                console.log(XMLHttpRequest,textStatus,errorThrown);
        }
    });

解决方案:

var data = JSON.parse(dapet.trim()); // Use trim() function in Javascript/Jquery

1
@ATS B,请尝试使用eval解析JSON。如果响应中没有错误,您可以在在线JSON解析器中进行检查。 - user3040610
使用JSON.parse()时,数据没有错误,我得到了正确的结果。 - Anil Talla
您的 JSON 格式正确,可以在 jsonlint.com 上进行验证。但是您的解析不正确。 - erikvimz
你能附上第一个 JSON 吗?你只附上了其中一个。 - user5260143
@anil。你可以在2.屏幕截图中看到Json 2 Json 2。 - ATS B
1个回答

1
尝试修剪第二个JSON。
$json = '{"setmargin":["20000","20000"],"setkodebarang":["TR-2C2C","TR-1086"],"post":["Belum","Belum"],"image":["www.site.com\/item_show.php?id=156801&flag=s1","www.site.com\/item_show.php?id=156800&flag=s1"],"hargajual":[125000,125000],"productlink":["www.site.com\/indexphp?hal=detail&id=156801&show=JAM COUPLE&nama=COUPLE ROLEX KULIT SILVER","www.site.com\/indexphp?hal=detail&id=156800&show=JAM COUPLE&nama=COUPLE ROLEX KULIT SILVER"],"detail":["JAMCOUPLE-JC53","JAMCOUPLE-JC50"],"price":["105000","105000"],"page":[]}';
$json = trim($json);

我尝试了这个,但什么也没有发生 $arr = array("setmargin"=>$setmargin,"setkodebarang"=>$setkodebarang,"fbpost"=>$fbpost,"image"=>$imagesurl,"hargajual"=>$hargajual,"productlink"=>$produckurl,"detail"=>$detail,"price"=>$priceprod,"page"=>$halamanSup);$json = json_encode($arr); echo trim($json); - ATS B
你也尝试过使用Jquery吗? 如果是,那么这一定是一个编码错误……在JSON.parse()中很常见…… - Samuel Fajreldines

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接