简体   繁体   English

字符串中特殊字符的 JSON.parse 和 JSON.stringify 错误

[英]JSON.parse and JSON.stringify error on special character in string

 console.log(JSON.stringify($('.btn').attr('data-obj')))
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-info user_action" data-action="edit_faqs" data-obj="{SysID:2,Artist:Json Mras,Song:I'm yours}" data-toggle="modal " data-target="#SongsModal "><i class="fa fa-edit "></i> Edit</button>

I found this but this is a little different in my case.我发现了这个,但在我的情况下这有点不同。

When I tried using JSON.stringify on the data I can see in the dev tools当我尝试对数据使用 JSON.stringify 时,我可以在开发工具中看到

//I want I'm Yours
"{\"Song\":\"I"

The data is stored in my database and I get it in ajax request and I want to use JSON.stringify to it because I will put it as a data-* on a button which will then be used when I click the button.数据存储在我的数据库中,我在 ajax 请求中获取它,我想对它使用JSON.stringify ,因为我将它作为data-*放在按钮上,然后在我单击按钮时使用。 On button click I want to use JSON.parse so I can iterate over it.单击按钮时,我想使用JSON.parse以便我可以对其进行迭代。

How to escape all special characters in JSON.stringify and JSON.parse如何转义JSON.stringifyJSON.parse所有特殊字符

Error is:错误是:

Uncaught SyntaxError: Unexpected end of JSON input未捕获的语法错误:JSON 输入意外结束

That points to the JSON.stringify那指向JSON.stringify

在此处输入图片说明

Added a snippet but it didnt recreate problem so I added an image添加了一个片段但它没有重新创建问题所以我添加了一个图像

Database:数据库:

在此处输入图片说明

Update:更新:

I get the data from ajax request.我从ajax请求中获取数据。 After getting the data I use data_array = JSON.stringify(data[i]);获取数据后,我使用data_array = JSON.stringify(data[i]); then apply the data as data-obj='" + data_array + "'然后将数据应用为data-obj='" + data_array + "'

That's not a problem with JSON, it's a problem with your misuse of HTML.这不是 JSON 的问题,而是您滥用 HTML 的问题。

You inserted a raw string into a HTML attribute, probably from a template, possibly by doing something like您将原始字符串插入到 HTML 属性中,可能来自模板,可能通过执行类似的操作

<button data-obj="<?php echo json_encode($data); ?>">

Or possibly in JavaScript或者可能在 JavaScript 中

container.innerHTML = '<button data-obj="' + JSON.stringify(data) + '">';

so your quotes end up clashing with HTML's.所以你的引号最终会与 HTML 冲突。 You need to change your quotes into HTML entities;您需要将引号更改为 HTML 实体; the minimum is changing double quotes " into &quot; if you are using double quotes around the HTML attribute (the most common scenario).如果您在 HTML 属性周围使用双引号(最常见的情况),则最低限度是将双引号"更改为&quot;

You have not told us how you insert the content into the HTML, so I can't tell you how to do it until you do clarify.您还没有告诉我们如何将内容插入到 HTML 中,所以在您澄清之前我无法告诉您如何去做。

EDIT: Apparently you are building HTML in JavaScript by concatenation.编辑:显然您是通过连接在 JavaScript 中构建 HTML。 In that case, the solution I showed would work:在这种情况下,我展示的解决方案将起作用:

container.innerHTML = '<button data-obj="' + data_array.replace(/"/g, '&quot;') + '">';

Or, if you're using single quotes to enclose the attribute (not commonly done, but certainly an option), you would need to change those to &apos;或者,如果你使用单引号括住属性(不常见,但肯定是一个选项),你将需要改变那些&apos; instead:反而:

container.innerHTML = "<button data-obj='" + data_array.replace(/'/g, '&apos;') + "'>";

However, given that it's JavaScript we're talking about, and it can work with DOM, and DOM knows what's what... there's many other solutions.然而,考虑到我们正在谈论的是 JavaScript,它可以与 DOM 一起工作,而且 DOM 知道什么是什么……还有很多其他的解决方案。 The most primitive one is to work with DOM directly:最原始的一种是直接使用 DOM:

let button = document.createElement('button');
button.setAttribute('data-obj', data_array);
container.appendChild(button);

When you do this, JavaScript manipulates the DOM directly, not HTML, so escaping is not needed.执行此操作时,JavaScript 直接操作 DOM,而不是 HTML,因此不需要转义。

A level above this would be to use a library like jQuery:高于此级别将使用像 jQuery 这样的库:

$('<button>').data('obj', data_array).appendTo('#container');

A level yet above that would be to use a library that employs data binding, like Angular, React, Vue, Ractive... where you would just set the value on your model, and the document automagically reflects that change.高于此的一个级别是使用采用数据绑定的库,例如 Angular、React、Vue、Ractive……您只需在模型上设置值,文档就会自动反映该更改。

Changing manually " into &quot; is only needed if you directly manipulate HTML.仅当您直接操作 HTML 时才需要手动将"更改为&quot;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM