Javascript JSON解析错误如何解决?

Javascript JSON解析错误如何解决?,javascript,jquery,json,Javascript,Jquery,Json,JS: JSBIN链接: 页脚注:此JSON是使用PHP函数JSON\u encode生成的。 Chrome控制台错误:JSON中位置41处出现意外数字 $.parseJSON不工作任何人都可以帮助我它为什么不工作?您应该只使用双引号字符串 var data=[{“userid”:“14”,“mobile”:“a:1:{i:0;s:10:\“992584244\”;},“宠物类型”:“发现”,“宠物猫”:“2”,“宠物品种”:“1”,“宠物龙”:“72.58522”,“宠物拉特”:“23.08

JS:

JSBIN链接:

页脚注:此JSON是使用PHP函数JSON\u encode生成的。

Chrome控制台错误:JSON中位置41处出现意外数字


$.parseJSON不工作任何人都可以帮助我它为什么不工作?

您应该只使用
双引号字符串

var data=[{“userid”:“14”,“mobile”:“a:1:{i:0;s:10:\“992584244\”;},“宠物类型”:“发现”,“宠物猫”:“2”,“宠物品种”:“1”,“宠物龙”:“72.58522”,“宠物拉特”:“23.087618”,“宠物奖励”:“货币”:(欧元)”,“姓名”:“日期”:“2017-03-27”,“宠物城”:“洛杉矶”,“pid”:“200”,“盖子”:“194”,“类型”:“发现”,“宠物位置”:“圣莫尼卡丢失”,“宠物丢失地址”:“dshtjh”,“宠物邮政”:“380009”,“宠物国家”:“美国”,“距离”:“0.1547392060658162”,“狗种”:null,“猫种”:“阿比西尼亚猫”,“img”:“a:1:{i:0;s:9:\'img1.jpeg\”;},“宠物hidenum”:“249310649”},{“用户ID”:“13”,“手机”:null,“宠物类型”:“丢失”,“宠物猫种”:“1”,“宠物品种”:“5”,“宠物长”72.582”,“宠物”宠物奖励:“23”,“货币:”€(欧元)”,“姓名:”€(欧元)”,“帕比”,“日期:”,“2017-04-27”,“宠物城:”,“艾哈迈达瓦德”,“pid:”,“233”,“盖子:”,“227”,“类型:”,“遗失”,“宠物遗失地点:”,“西夫兰贾尼”,“宠物遗失地址:”,“艾哈迈达瓦德”,“宠物邮寄:”,“dmart”,“宠物国家:”,“印度”,“距离:”,“0.15473920658162”,“狗种:”,“艾迪”,“猫种:”,“猫种:”,“空”,img:“a:4:{i:0;s:9:“img1”i:1;s:9:“img2.jpeg”;“i:2”;s:8:“img3.png”;“i:3;s:9:“img4.jpeg”;“宠物hidenum”;“730117427”;“{”用户ID:“13”,“手机”:空,“宠物类型”:“发现”,“宠物猫”:“2”,“宠物品种”:“7”,“宠物龙”:“72.58522”,“宠物拉特”:“23.087618”,“宠物奖励”:“货币”:(欧元)”,“姓名”:“2017年4月4日”,“宠物类型”:“a”,“宠物城市”:“发现”;“宠物种类”:“228宠物丢失地点:“asds”,“宠物丢失地址”:“asdsffas”,“宠物邮政”:“qwer”,“宠物国家”:“印度”,“距离”:“0.15473920658162”,“狗种”:null,“猫种”:“亚洲猫”,“img”:“a:4:{i:0;s:9:\“img1.jpeg\”;i:1;s:8:“img2.png\”;i:2;s:9:\“img3.jpeg\”;i:3;s:8:“img4.png\”;“img4.png\”;“img4.png\”;“userid”;“125U:“宠物丢失类型”:“IMG3553:{宠物猫“:”2“,”宠物品种“:”511“,”宠物长“:”72.58522“,”宠物猫“:”23.087618“,”宠物猫奖励“:”1“,”货币“:”$(美元)”,”姓名“:”宠物猫“,”日期“:”1970-01-01“,”宠物城“:”艾哈迈达巴德“,”pid“:”236“,”盖子“:”230“,”类型“,”丢失“,”宠物猫丢失地点“:”加利福尼亚“,”宠物丢失地址“:”12圣地亚哥“,”宠物猫邮寄“:”380008“,”宠物狗国“:”美国“,”距离“,”0.1547392064658162,“狗品种”:空,“猫品种”:“未知”,“img”:“a:1:{i:0;s:9:\'img1.jpeg\”;}”,“宠物品种”:“501387450”});
var stringified=JSON.stringify(数据);
控制台日志(字符串化);

//这是一个有效的JSON字符串,现在您可以使用JSON.parse(stringified)
使用JSON.parse(response);它似乎是一个多行字符串,只允许通过字符串模板文本使用多行字符串。JSOn允许**双引号**,而您使用的是单引号。您是否尝试过在JS中删除起始引号和结束引号,而不是使用JSOn.parse等?类似于
var data=response.replace(/(^')|('$)/g'))
。不确定,但您可以尝试一次。与其进行向下投票,不如尝试解释一下您不同意的原因。我没有进行向下投票,但这并不能解决OPs问题,因为您颠倒了逻辑。您将数组编码为JSON,而不是解码字符串。哈哈……这只是一个示例,
JSON\u encode
正是这样做的。我想我突出显示了我的评论和回答中的所有问题。好吧,但是它有什么关系呢?OP是用PHP编码的,并且已经(我可能会错误地添加)使用了双引号字符串。
    var response = '[{  
    "userid":"14",
    "mobile":"a:1:{i:0;s:10:\"9925847424\";}",
    "pet_type":"Found",
    "pet_cat":"2",
    "petbreed":"1",
    "pet_long":"72.58522",
    "pet_lat":"23.087618",
    "pet_reward":"",
    "currency":"\u20ac (EUR)",
    "name":"",
    "date":"2017-03-27",
    "pet_city":"Los Angelese",
    "pid":"200",
    "lid":"194",
    "type":"Found",
    "pet_lost_location":"Santa Monica",
    "pet_lost_address":" dshtjh",
    "pet_postal":"380009",
    "pet_country":"United states",
    "distance":"0.15473920640658162",
    "dogbreed":null,
    "catbreed":"Abyssinian cat",
    "img":"a:1:{i:0;s:9:\"img1.jpeg\";}",
    "pet_hidenum":"249310649"
},
{  
    "userid":"13",
    "mobile":null,
    "pet_type":"Lost",
    "pet_cat":"1",
    "petbreed":"5",
    "pet_long":"72.58522",
    "pet_lat":"23.087618",
    "pet_reward":"23",
    "currency":"\u20ac (EUR)",
    "name":"pappy",
    "date":"2017-04-27",
    "pet_city":"ahmedavad",
    "pid":"233",
    "lid":"227",
    "type":"Lost",
    "pet_lost_location":"sivranjani",
    "pet_lost_address":"ahmedavad",
    "pet_postal":" dmart",
    "pet_country":"India",
    "distance":"0.15473920640658162",
    "dogbreed":"Aidi",
    "catbreed":null,
    "img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:9:\"img2.jpeg\";i:2;s:8:\"img3.png\";i:3;s:9:\"img4.jpeg\";}",
    "pet_hidenum":"730117427"
},
{  
    "userid":"13",
    "mobile":null,
    "pet_type":"Found",
    "pet_cat":"2",
    "petbreed":"7",
    "pet_long":"72.58522",
    "pet_lat":"23.087618",
    "pet_reward":"",
    "currency":"\u20ac (EUR)",
    "name":"",
    "date":"2017-04-27",
    "pet_city":"a",
    "pid":"234",
    "lid":"228",
    "type":"Found",
    "pet_lost_location":"asds",
    "pet_lost_address":"asdsffas",
    "pet_postal":"qwer",
    "pet_country":"India",
    "distance":"0.15473920640658162",
    "dogbreed":null,
    "catbreed":"Asian cat",
    "img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:8:\"img2.png\";i:2;s:9:\"img3.jpeg\";i:3;s:8:\"img4.png\";}",
    "pet_hidenum":"358512553"
},
{  
    "userid":"16",
    "mobile":"",
    "pet_type":"Lost",
    "pet_cat":"2",
    "petbreed":"511",
    "pet_long":"72.58522",
    "pet_lat":"23.087618",
    "pet_reward":"1",
    "currency":"$ (USD)",
    "name":"Cade1",
    "date":"1970-01-01",
    "pet_city":"Ahmedabad",
    "pid":"236",
    "lid":"230",
    "type":"Lost",
    "pet_lost_location":"California",
    "pet_lost_address":"12 San Diego",
    "pet_postal":"380008",
    "pet_country":"United states",
    "distance":"0.15473920640658162",
    "dogbreed":null,
    "catbreed":"Unknown",
    "img":"a:1:{i:0;s:9:\"img1.jpeg\";}",
    "pet_hidenum":"501387450"
}]';
res = $.parseJSON(response);