我有一个API,它返回一个包含数组数组的字符串。数组包含字符串,如下所示:
" ['猫''小鼠'],['鸭''鱼'],[& #39;狮''斑马']]"
我希望将其转换为JSON对象。
我已尝试过JSON.parse,但它给了我一个错误
块引用 [[' +00:00(Etc / GMT)'' +00:00(Etc / GMT)'],[' +03:00(欧洲/加里宁格勒)',' 03:0 ^ SyntaxError:意外的令牌'
我试图变成一个对象的字符串就是这个:
"[['+00:00 (Etc/GMT)','+00:00 (Etc/GMT)'],['+03:00 (Europe/Kaliningrad)','+03:00 (Europe/Kaliningrad)'],['-01:00 (Etc/GMT+1)','-01:00 (Etc/GMT+1)'],['AKST (America/Anchorage)','AKST (America/Anchorage)'],['ART (America/Argentina/Buenos_Aires)','ART (America/Argentina/Buenos_Aires)'],['AST (America/Puerto_Rico)','AST (America/Puerto_Rico)'],['AST (Asia/Baghdad)','AST (Asia/Baghdad)'],['AST (Atlantic/Bermuda)','AST (Atlantic/Bermuda)'],['BRT (America/Sao_Paulo)','BRT (America/Sao_Paulo)'],['CET (CET)','CET (CET)'],['CET (Europe/Amsterdam)','CET (Europe/Amsterdam)'],['CET (Europe/Brussels)','CET (Europe/Brussels)'],['CET (Europe/Budapest)','CET (Europe/Budapest)'],['CET (Europe/Madrid)','CET (Europe/Madrid)'],['CET (Europe/Oslo)','CET (Europe/Oslo)'],['CET (Europe/Paris)','CET (Europe/Paris)'],['CET (Europe/Prague)','CET (Europe/Prague)'],['CET (Europe/Vienna)','CET (Europe/Vienna)'],['CET (Europe/Warsaw)','CET (Europe/Warsaw)'],['CET (Europe/Zurich)','CET (Europe/Zurich)'],['CLST (America/Santiago)','CLST (America/Santiago)'],['COT (America/Bogota)','COT (America/Bogota)'],['CST (America/Chicago)','CST (America/Chicago)'],['CST (America/Costa_Rica)','CST (America/Costa_Rica)'],['CST (America/Mexico_City)','CST (America/Mexico_City)'],['CST (Asia/Shanghai)','CST (Asia/Shanghai)'],['CST (Asia/Taipei)','CST (Asia/Taipei)'],['CST (Australia/Adelaide)','CST (Australia/Adelaide)'],['EAT (Africa/Nairobi)','EAT (Africa/Nairobi)'],['EET (EET)','EET (EET)'],['EET (Europe/Istanbul)','EET (Europe/Istanbul)'],['EET (Europe/Kiev)','EET (Europe/Kiev)'],['EST (America/Indiana/Indianapolis)','EST (America/Indiana/Indianapolis)'],['EST (America/New_York)','EST (America/New_York)'],['EST (Australia/Brisbane)','EST (Australia/Brisbane)'],['EST (Australia/Melbourne)','EST (Australia/Melbourne)'],['EST (Australia/Sydney)','EST (Australia/Sydney)'],['GMT (Europe/Dublin)','GMT (Europe/Dublin)'],['GMT (Europe/London)','GMT (Europe/London)'],['GST (Asia/Dubai)','GST (Asia/Dubai)'],['HKT (Asia/Hong_Kong)','HKT (Asia/Hong_Kong)'],['HST (Pacific/Honolulu)','HST (Pacific/Honolulu)'],['ICT (Asia/Bangkok)','ICT (Asia/Bangkok)'],['IST (Asia/Jerusalem)','IST (Asia/Jerusalem)'],['IST (Asia/Kolkata)','IST (Asia/Kolkata)'],['JST (Asia/Tokyo)','JST (Asia/Tokyo)'],['KST (Asia/Seoul)','KST (Asia/Seoul)'],['MSK (Europe/Moscow)','MSK (Europe/Moscow)'],['MST (America/Denver)','MST (America/Denver)'],['MST (America/Phoenix)','MST (America/Phoenix)'],['MST (MST)','MST (MST)'],['MYT (Asia/Kuala_Lumpur)','MYT (Asia/Kuala_Lumpur)'],['NZDT (Pacific/Auckland)','NZDT (Pacific/Auckland)'],['PET (America/Lima)','PET (America/Lima)'],['PHT (Asia/Manila)','PHT (Asia/Manila)'],['PST (America/Los_Angeles)','PST (America/Los_Angeles)'],['PYST (America/Asuncion)','PYST (America/Asuncion)'],['SAST (Africa/Johannesburg)','SAST (Africa/Johannesburg)'],['SGT (Asia/Singapore)','SGT (Asia/Singapore)'],['TJT (Asia/Dushanbe)','TJT (Asia/Dushanbe)'],['UTC (UTC)','UTC (UTC)'],['VET (America/Caracas)','VET (America/Caracas)'],['WET (Europe/Lisbon)','WET (Europe/Lisbon)'],['WST (Australia/Perth)','WST (Australia/Perth)']]"
答案 0 :(得分:8)
这不是有效的JSON。您必须将'
个字符替换为"
。
答案 1 :(得分:0)
您的JSON无效,如第一个答案中所述,但这是一个快速修复。
说你有:
a = "[['+00:00 (Etc/GMT)','+00:00 (Etc/GMT)'],['+03:00 (Europe/Kalini..
然后将'
替换为"
:
a = a.replace(/'/g, '"');
然后将其解析为JSON:
a = jQuery.parseJSON(a)
然后你有一个有效的JSON ,尝试console.table(a)
来查看你的有效json !