You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON.parse解析转义字符串时出现Unexpected token错误排查

JSON.parse解析时Unexpected token错误(关联description字段)的分析与解决

错误原因

你用ES6模板字符串(反引号``)包裹JSON字符串时,模板字符串会自动解析内部的\"转义符,将其转换为普通双引号"。这就导致description字段的内容里出现了未转义的双引号,直接违反了JSON的语法规则,触发Unexpected token错误。

解决办法

有两种可行的修复方式:

方式一:改用普通单引号包裹JSON字符串

单引号不会解析内部的\"转义符,能保留JSON所需的转义格式:

let str = '{"kinopoisk_url":"https:\/\/www.themoviedb.org\/movie\/1845","tmdb_id":"1845","name":"2 Days in Paris","o_name":"2 Days in Paris","cover_big":"https:\/\/cportal.unibox.us:2087\/images\/4yf8vcgJtADTTPP0Ltq2KhqLfdE_big.jpg","movie_image":"https:\/\/cportal.unibox.us:2087\/images\/4yf8vcgJtADTTPP0Ltq2KhqLfdE_big.jpg","releasedate":"2007-02-09","episode_run_time":0,"youtube_trailer":"","director":"Julie Delpy","actors":"Julie Delpy, Adam Goldberg, Daniel Br\u00fchl, Adan Jodorowsky, Alexandre Nahon, Albert Delpy, Alexia Landeau, Vanessa Seward","cast":"Julie Delpy, Adam Goldberg, Daniel Br\u00fchl, Adan Jodorowsky, Alexandre Nahon, Albert Delpy, Alexia Landeau, Vanessa Seward","description":"Adam Goldberg delivers \"an uproarious study in transatlantic culture panic\" as Jack, an anxious, hypochondriac-prone New Yorker vacationing throughout Europe with his breezy, free-spirited Parisian girlfriend, Marion. But when they make a two-day stop in Marion's hometown, the couple's romantic trip takes a turn as Jack is exposed to Marion's sexually perverse and emotionally unstable family.","plot":"Adam Goldberg delivers \"an uproarious study in transatlantic culture panic\" as Jack, an anxious, hypochondriac-prone New Yorker vacationing throughout Europe with his breezy, free-spirited Parisian girlfriend, Marion. But when they make a two-day stop in Marion's hometown, the couple's romantic trip takes a turn as Jack is exposed to Marion's sexually perverse and emotionally unstable family.","age":"12+","mpaa_rating":"","rating_count_kinopoisk":253,"country":"France, Germany","genre":"Comedy \/ Romance","backdrop_path":["https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_0.jpg","https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_1.jpg"],"duration_secs":6012,"duration":"01:40:12","video":{"index":1,"codec_name":"h264","codec_long_name":"H.264 \/ AVC \/ MPEG-4 AVC \/ MPEG-4 part 10","profile":"High","codec_type":"video","codec_time_base":"1001\/48000","codec_tag_string":"[0][0][0][0]","codec_tag":"0x0000","width":1280,"height":698,"coded_width":1280,"coded_height":704,"has_b_frames":2,"sample_aspect_ratio":"1:1","display_aspect_ratio":"640:349","pix_fmt":"yuv420p","level":31,"chroma_location":"left","field_order":"progressive","refs":1,"is_avc":"true","nal_length_size":"4","r_frame_rate":"24000\/1001","avg_frame_rate":"24000\/1001","time_base":"1\/1000","start_pts":0,"start_time":"0.000000","bits_per_raw_sample":"8","disposition":{"default":1,"dub":0,"original":0,"comment":0,"lyrics":0,"karaoke":0,"forced":0,"hearing_impaired":0,"visual_impaired":0,"clean_effects":0,"attached_pic":0,"timed_thumbnails":0},"tags":{"title":"Ganool.com","DURATION":"01:40:12.672000000"}},"audio":{"index":0,"codec_name":"aac","codec_long_name":"AAC (Advanced Audio Coding)","profile":"HE-AAC","codec_type":"audio","codec_time_base":"1\/48000","codec_tag_string":"[0][0][0][0]","codec_tag":"0x0000","sample_fmt":"fltp","sample_rate":"48000","channels":2,"channel_layout":"stereo","bits_per_sample":0,"r_frame_rate":"0\/0","avg_frame_rate":"0\/0","time_base":"1\/1000","start_pts":31,"start_time":"0.031000","disposition":{"default":1,"dub":0,"original":0,"comment":0,"lyrics":0,"karaoke":0,"forced":0,"hearing_impaired":0,"visual_impaired":0,"clean_effects":0,"attached_pic":0,"timed_thumbnails":0},"tags":{"language":"eng","title":"Ganool.com","DURATION":"01:40:12.745000000"}},"bitrate":1048,"rating":"6.3","backdrop":"https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_0.jpg\r\nhttps:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_1.jpg\r\n","rating_mpaa":"R"}';
console.log(JSON.parse(str));

方式二:保留模板字符串,对转义引号再次转义

如果必须使用模板字符串,需要将内部的\"替换为\\",这样模板字符串解析后会保留\",符合JSON的转义要求:

let str = `{"kinopoisk_url":"https:\/\/www.themoviedb.org\/movie\/1845","tmdb_id":"1845","name":"2 Days in Paris","o_name":"2 Days in Paris","cover_big":"https:\/\/cportal.unibox.us:2087\/images\/4yf8vcgJtADTTPP0Ltq2KhqLfdE_big.jpg","movie_image":"https:\/\/cportal.unibox.us:2087\/images\/4yf8vcgJtADTTPP0Ltq2KhqLfdE_big.jpg","releasedate":"2007-02-09","episode_run_time":0,"youtube_trailer":"","director":"Julie Delpy","actors":"Julie Delpy, Adam Goldberg, Daniel Br\u00fchl, Adan Jodorowsky, Alexandre Nahon, Albert Delpy, Alexia Landeau, Vanessa Seward","cast":"Julie Delpy, Adam Goldberg, Daniel Br\u00fchl, Adan Jodorowsky, Alexandre Nahon, Albert Delpy, Alexia Landeau, Vanessa Seward","description":"Adam Goldberg delivers \\"an uproarious study in transatlantic culture panic\\" as Jack, an anxious, hypochondriac-prone New Yorker vacationing throughout Europe with his breezy, free-spirited Parisian girlfriend, Marion. But when they make a two-day stop in Marion's hometown, the couple's romantic trip takes a turn as Jack is exposed to Marion's sexually perverse and emotionally unstable family.","plot":"Adam Goldberg delivers \\"an uproarious study in transatlantic culture panic\\" as Jack, an anxious, hypochondriac-prone New Yorker vacationing throughout Europe with his breezy, free-spirited Parisian girlfriend, Marion. But when they make a two-day stop in Marion's hometown, the couple's romantic trip takes a turn as Jack is exposed to Marion's sexually perverse and emotionally unstable family.","age":"12+","mpaa_rating":"","rating_count_kinopoisk":253,"country":"France, Germany","genre":"Comedy \/ Romance","backdrop_path":["https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_0.jpg","https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_1.jpg"],"duration_secs":6012,"duration":"01:40:12","video":{"index":1,"codec_name":"h264","codec_long_name":"H.264 \/ AVC \/ MPEG-4 AVC \/ MPEG-4 part 10","profile":"High","codec_type":"video","codec_time_base":"1001\/48000","codec_tag_string":"[0][0][0][0]","codec_tag":"0x0000","width":1280,"height":698,"coded_width":1280,"coded_height":704,"has_b_frames":2,"sample_aspect_ratio":"1:1","display_aspect_ratio":"640:349","pix_fmt":"yuv420p","level":31,"chroma_location":"left","field_order":"progressive","refs":1,"is_avc":"true","nal_length_size":"4","r_frame_rate":"24000\/1001","avg_frame_rate":"24000\/1001","time_base":"1\/1000","start_pts":0,"start_time":"0.000000","bits_per_raw_sample":"8","disposition":{"default":1,"dub":0,"original":0,"comment":0,"lyrics":0,"karaoke":0,"forced":0,"hearing_impaired":0,"visual_impaired":0,"clean_effects":0,"attached_pic":0,"timed_thumbnails":0},"tags":{"title":"Ganool.com","DURATION":"01:40:12.672000000"}},"audio":{"index":0,"codec_name":"aac","codec_long_name":"AAC (Advanced Audio Coding)","profile":"HE-AAC","codec_type":"audio","codec_time_base":"1\/48000","codec_tag_string":"[0][0][0][0]","codec_tag":"0x0000","sample_fmt":"fltp","sample_rate":"48000","channels":2,"channel_layout":"stereo","bits_per_sample":0,"r_frame_rate":"0\/0","avg_frame_rate":"0\/0","time_base":"1\/1000","start_pts":31,"start_time":"0.031000","disposition":{"default":1,"dub":0,"original":0,"comment":0,"lyrics":0,"karaoke":0,"forced":0,"hearing_impaired":0,"visual_impaired":0,"clean_effects":0,"attached_pic":0,"timed_thumbnails":0},"tags":{"language":"eng","title":"Ganool.com","DURATION":"01:40:12.745000000"}},"bitrate":1048,"rating":"6.3","backdrop":"https:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_0.jpg\r\nhttps:\/\/cportal.unibox.us:2087\/images\/1845_movie_backdrop_1.jpg\r\n","rating_mpaa":"R"}`;
console.log(JSON.parse(str));

内容的提问来源于stack exchange,提问作者Mike woods

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:36:28