JSON.parse:意外字符
JSON.parse: unexpected character
我有两个来自php代码的json。我认为这是一样的。
1.json 1
{"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":[]}
当我在javascript中使用JSON.parse进行解析时,Json 1
没有问题。但是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
尝试修剪第二个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);
相关文章:
- 使用提取文本webpack插件时出现意外字符
- AJAX语法错误:JSON.parse:意外字符
- JavaScript意外的令牌<,有什么方法可以逃避错误(或至少跳过那些无用的字符)
- 代码点火器:搜索自动完成 JSON 解析意外字符
- 浏览器抛出错误:解析文件时出现意外字符 ' ' (1:0):图像.png
- JSON.parse:意外字符
- "JSON_PARSING_ERROR:位置0处的意外字符(d);当从GAS发送GCM请求时
- SyntaxError:JSON.parse:登录脚本中出现意外字符
- JSON和意外字符
- Uglify JS错误:意外字符''连接时,请缩小select2
- JSON.parse:Javascript中出现意外的字符错误
- 奇怪的javascript行为-JSON.parse:意外字符
- Vimeo Slider脚本-SyntaxError:JSON.parse:意外字符
- asp.net Jquery.ajax保留JSON.分析意外字符错误
- JSON解析意外的非空白字符
- ExecJS::P rogramError: 意外字符 '#'
- 语法错误:JSON.parse:意外字符
- (仅在IE中)SCRIPT65535:对方法或属性访问的意外调用.主.js,第 152 行字符 28
- 错误:“JSON.parse:JSON 数据第 1 行第 1 列的意外字符”,当包含其他 php 文件时
- JSON 数据后出现意外的非空格字符