javascript - JSON.parse 在尝试解析转义双引号时抛出语法错误

标签 javascript php json parsing

我似乎无法弄清楚为什么会这样。我有一个数组,我将它作为 Json 字符串回显到我页面中动态创建的 JavaScript 标记中。

这是 PHP 代码:

echo '<script type="text/javascript">var Lang = \'';
echo str_replace('\'', '\\\'', json_encode($this->Lang));
echo '\';Lang = JSON.parse(Lang);</script>';

但是当 JavaScript 解析字符串时,我得到这个错误:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 162 of the JSON data

这通常意味着我的 Json 字符串中存在语法错误,但我找不到此语法错误。

这是这个脚本标签在浏览器中的样子:

<script type="text/javascript">var Lang = '{"Code":"en","Layout":"LtR","Error_NoPassword":"Password is Required","Error_NoUsername":"Username is Required","Header_Notification_Comment":"[A] commented on \"[O]\"","Header_Notification_Follow":"[A] is now following \"[O]\"","Header_Notification_Like":"[A] liked \"[O]\"","Header_Notification_Message":"[A] sent you a message.","PLogin_Login_Fail":"Login failed","PLogin_Login_ForgotLink":"Click here if you forgot your password !","PLogin_Multiverse_Description_Text":"Multiverse is an application developed for Story writers of Comics, Games, Movies, Novels and etc. This application uses \"Snowflake Method\" to make things easier for the author.","PLogin_Multiverse_Description_Title":"What is Multiverse ?","PLogin_ResetPassword_Title":"Reset Password","PLogin_ResetPassword_Description":"Enter the email address you use on Multiverse. Your email must have been verified after you signed up. If you didn\'t verify your email address, We\'re afraid there\'s nothing we can do at this point.","PLogin_ResetPassword_MailSent":"In case you have entered your email address correctly, And it is verified, Then an email was sent to your address containing a single-use link to reset your password.","PLogin_SrcError_Account":"In order to have an account of your own, You need to sign up first !","PLogin_SrcError_Edit":"In order to create your own content, You need to create an account first !","PLogin_SrcError_People":"In order to interact with other people on Multiverse, You need to create an account first !","PLogin_Signup_UsernameWillBe":"Your username will be : ","Signup_Fail_Captcha":"Captcha Failed !","Signup_Fail_Email":"Incorrect email address","Signup_Fail_Password":"It is recommended for password to be 8 or more characters","Signup_Fail_Unknown":"Registration failed for unknown reason","Signup_Fail_UsernameTaken":"Username is already taken","Signup_Fail_UsernameInvalid":"Username is Invalid.","Signup_Fail_Realname":"Enter your Name","Signup_Title":"Become an Author now","Signup_ExploreButton":"Explore as Guest !","UniExp_Info_IsPublic":"Public","UniExp_Info_Description":"Description","UniExp_Info_Language":"Language","UniExp_SelectedInfo_NoTimeline":"No Timeline Selected","UniExp_SelectedInfo_NoStory":"No Story Selected","Word_Account":"Account","Word_Act":"Act","Word_Character":"Character","Word_ContactUs":"Contact Us","Word_Editors":"Editors","Word_EmailAddress":"Email Address","Word_Explore":"Explore","Word_ID":"ID","Word_License":"License","Word_Login":"Log In","Word_Logout":"Sign Out","Word_MobileVersion":"Mobile Version","Word_MyUniverses":"My Universes","Word_Name":"Name","Word_No":"No","Word_Owner":"Owner","Word_Part":"Part","Word_Password":"Password","Word_People":"People","Word_Plot":"Plot","Word_Realname":"Real Name","Word_Scene":"Scene","Word_Search":"Search","Word_Select":"Select","Word_Signup":"Sign up","Word_Story":"Story","Word_Timeline":"Timeline","Word_Unknown":"Unknown","Word_Universe":"Universe","Word_Username":"Username","Word_You":"You","Word_Yes":"Yes"}';Lang = JSON.parse(Lang);</script>

为了可读性,我在此处放置了 Json 字符串的 PRETTY_PRINT 版本:

{
"Code": "en",
"Layout": "LtR",
"Error_NoPassword": "Password is Required",
"Error_NoUsername": "Username is Required",
"Header_Notification_Comment": "[A] commented on \"[O]\"",
"Header_Notification_Follow": "[A] is now following \"[O]\"",
"Header_Notification_Like": "[A] liked \"[O]\"",
"Header_Notification_Message": "[A] sent you a message.",
"PLogin_Login_Fail": "Login failed",
"PLogin_Login_ForgotLink": "Click here if you forgot your password !",
"PLogin_Multiverse_Description_Text": "Multiverse is an application developed for Story writers of Comics, Games, Movies, Novels and etc. This application uses \"Snowflake Method\" to make things easier for the author.",
"PLogin_Multiverse_Description_Title": "What is Multiverse ?",
"PLogin_ResetPassword_Title": "Reset Password",
"PLogin_ResetPassword_Description": "Enter the email address you use on Multiverse. Your email must have been verified after you signed up. If you didn\'t verify your email address, We\'re afraid there\'s nothing we can do at this point.",
"PLogin_ResetPassword_MailSent": "In case you have entered your email address correctly, And it is verified, Then an email was sent to your address containing a single-use link to reset your password.",
"PLogin_SrcError_Account": "In order to have an account of your own, You need to sign up first !",
"PLogin_SrcError_Edit": "In order to create your own content, You need to create an account first !",
"PLogin_SrcError_People": "In order to interact with other people on Multiverse, You need to create an account first !",
"PLogin_Signup_UsernameWillBe": "Your username will be : ",
"Signup_Fail_Captcha": "Captcha Failed !",
"Signup_Fail_Email": "Incorrect email address",
"Signup_Fail_Password": "It is recommended for password to be 8 or more characters",
"Signup_Fail_Unknown": "Registration failed for unknown reason",
"Signup_Fail_UsernameTaken": "Username is already taken",
"Signup_Fail_UsernameInvalid": "Username is Invalid.",
"Signup_Fail_Realname": "Enter your Name",
"Signup_Title": "Become an Author now",
"Signup_ExploreButton": "Explore as Guest !",
"UniExp_Info_IsPublic": "Public",
"UniExp_Info_Description": "Description",
"UniExp_Info_Language": "Language",
"UniExp_SelectedInfo_NoTimeline": "No Timeline Selected",
"UniExp_SelectedInfo_NoStory": "No Story Selected",
"Word_Account": "Account",
"Word_Act": "Act",
"Word_Character": "Character",
"Word_ContactUs": "Contact Us",
"Word_Editors": "Editors",
"Word_EmailAddress": "Email Address",
"Word_Explore": "Explore",
"Word_ID": "ID",
"Word_License": "License",
"Word_Login": "Log In",
"Word_Logout": "Sign Out",
"Word_MobileVersion": "Mobile Version",
"Word_MyUniverses": "My Universes",
"Word_Name": "Name",
"Word_No": "No",
"Word_Owner": "Owner",
"Word_Part": "Part",
"Word_Password": "Password",
"Word_People": "People",
"Word_Plot": "Plot",
"Word_Realname": "Real Name",
"Word_Scene": "Scene",
"Word_Search": "Search",
"Word_Select": "Select",
"Word_Signup": "Sign up",
"Word_Story": "Story",
"Word_Timeline": "Timeline",
"Word_Unknown": "Unknown",
"Word_Universe": "Universe",
"Word_Username": "Username",
"Word_You": "You",
"Word_Yes": "Yes"
}

如错误中所述,我的 Json 数据的第 162 列是第一次出现转义双引号 (\"),但它似乎没有任何问题!

特别是当 PHP 自己的函数 (json_encode) 给我的时候!

这里有什么问题?

(我的浏览器是 Ubuntu Mate 16.04 上的 Firefox Quantum ...)

最佳答案

使用 json_encode 时,你不需要转义任何东西,它会生成有效的 json。您也不需要将其作为字符串添加,然后使用 javascript 再次解析它。

例如你会这样做:

<?php
// a mock of your object
$_this = new class {
    public $Code = 'en';
};

echo sprintf(
    '<script type="text/javascript">var Lang=%s;</script>', 
    json_encode($_this)
);

结果如下:

<script type="text/javascript">var Lang={"Code":"en"};</script>

https://3v4l.org/qQunD

关于javascript - JSON.parse 在尝试解析转义双引号时抛出语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48249470/

相关文章:

javascript - 如何使用javascript隐藏一个类?

javascript - 为什么我不能将对象添加到以下数组?

php - 使用 PHP 和 RESTful API 获取和发送文件

jquery - 使用 jquery .ajax 中的 dataFilter 和 json 数据

c# - 将原始 JSON 字符串存储在反序列化的 JSON.NET 对象中

javascript - 设置 jQuery .offset();而 "display:none;"会产生与 "display:block;"不同的其他左侧值和顶部值

javascript - 在 Angular 中从 nodejs 获取 JSON

php - 是否有任何自定义密码验证器? zend框架

php - httpd.conf 文件在哪里?

json - jackson JSON 格式不正确