【问题标题】:JSON parsing error how to resolve?JSON解析错误如何解决?
【发布时间】:2017-05-18 14:55:48
【问题描述】:

JS:

    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);

JSBIN 链接:https://jsbin.com/rarizavocu/edit?js,output

页脚说明:此 JSON 是使用 PHP 函数 json_encode 生成的。

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

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

【问题讨论】:

  • 使用 JSON.parse(response);
  • 好像是多行字符串,多行字符串只能通过字符串模板文字developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。 JSOn 允许 ** 双引号** 而您使用的是单引号。
  • 您是否尝试过在 JS 中删除开始和结束引号,而不是使用 JSON.parse 等?类似var data = response.replace(/(^')|('$)/g, '');。不确定,但您可以尝试一次。

标签: javascript jquery json


【解决方案1】:

你应该只使用double quoted strings

var data = [{"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":"€ (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":"€ (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":"€ (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"}];


var stringified = JSON.stringify(data);
console.log(stringified);
// this is a valid JSON String, now you can JSON.parse(stringified)

【讨论】:

  • 不要投反对票,试着解释你为什么不同意。
  • 我没有投反对票,但这并不能回答 OPs 问题,因为您已经颠倒了逻辑。您将数组编码为JSON,而不是解码字符串。
  • 大声笑...这只是一个例子,json_encode 正是这样做的。我想我在评论和回答中强调了所有问题。
  • 好的,但它有什么关系呢? OP 正在 PHP 中编码,并且已经(我可能会错误地添加)使用双引号字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2019-12-19
  • 2014-01-09
  • 2016-02-28
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
相关资源
最近更新 更多