包含json的数据属性中的单引号。

时间:2022-02-18 22:29:39

Cosmetic question: I have a html element containing possible dimensions for some embedded images, these are stored as:

修饰性问题:我有一个html元素,包含一些嵌入图像的可能维度,这些元素存储为:

<div class="inside" data-dimensions='{ "s-x": 213, "s-y": 160, "m-x": ...

I get out the data-dimension and parse with jQuery.parseJSON(jQuery.data("dimensions")) all fine and closely following the jquery's doc.

我取出数据维度并使用jquery . parsejson (jquery .data(“dimensions”))解析数据,所有这些都很好,并且与jquery的doc密切相关。

However I'm used to encapsulate all my html attributes inside double quotes:

然而,我习惯将我所有的html属性封装在双引号内:

<div class="inside" data-dimensions="{ 's-x': 213, 's-y': 160, 'm-x': ...

But then I get a malformed json exception. Are there ways so i can obey my self imposed "double quoted html attributes" law?

然后我得到一个异常的json异常。是否有办法让我遵守自己强加的“双重引用的html属性”法律?

5 个解决方案

#1


20  

You can use &quot; instead of ". But quoting orgies are horrible (in HTML even more than in PHP) so better go with single-quoting your html attributes.

您可以使用“而不是“。但是引用orgies是很可怕的(在HTML中甚至比在PHP中更可怕),所以最好使用单引号的HTML属性。

BTW, you do not need to use .parseJSON - jQuery does that automatically if the data- attribute starts with { (actually, it's more complex - here's the regex it uses to test if it should be parsed as JSON: ^(?:\{.*\}|\[.*\])$).

顺便说一句,你不需要使用.parseJSON -自动jQuery,如果数据属性开始{(实际上,它更复杂——这是它使用的正则表达式测试是否应该作为JSON解析:^(?:\ { . * \ } | \[* \]。)$)。

#2


9  

The JSON specification stipulates that keys and (string) values be quoted with double-quotes.

JSON规范规定了使用双引号引用的键和(字符串)值。

HTML attributes can be enclosed in either single or double quotes.

HTML属性可以包含在单引号或双引号中。

Personally, I wouldn't fight it and just go with what causes the least amount of friction and is easiest for all to understand which in this case is to single quote the HTML attributes and use double-quotes inside the attribute value.

就我个人而言,我不会反对它,我只是选择导致最少摩擦的东西,并且最容易理解在这种情况下哪个是单引号HTML属性,并在属性值中使用双引号。

#3


9  

Try this and you can keep nicely formatted JSON in the attribute:

尝试一下,您可以在属性中保留格式良好的JSON:

$.parseJSON($('.inside').data('dimensions').replace(/'/g,"\""))

#4


4  

Use &quot; instead of " and &apos; instead of '.

使用“;而不是“and &apos”;而不是”。

#5


0  

Another way is to use the function eval or create a new function. You can use either " or ' in your JSON and you don't need to put " around the keys in the JSON object.

另一种方法是使用函数eval或创建一个新函数。您可以在JSON中使用“或”,也可以不需要“在JSON对象中放置”。

let el = document.getElementById('example');
let person = (new Function(`return ${el.dataset.person}`))();
console.log(person);
person.age++;
console.log(person.age);
<div data-person="{name:'Natalie Portman',age:35}" id="example"></div>

#1


20  

You can use &quot; instead of ". But quoting orgies are horrible (in HTML even more than in PHP) so better go with single-quoting your html attributes.

您可以使用“而不是“。但是引用orgies是很可怕的(在HTML中甚至比在PHP中更可怕),所以最好使用单引号的HTML属性。

BTW, you do not need to use .parseJSON - jQuery does that automatically if the data- attribute starts with { (actually, it's more complex - here's the regex it uses to test if it should be parsed as JSON: ^(?:\{.*\}|\[.*\])$).

顺便说一句,你不需要使用.parseJSON -自动jQuery,如果数据属性开始{(实际上,它更复杂——这是它使用的正则表达式测试是否应该作为JSON解析:^(?:\ { . * \ } | \[* \]。)$)。

#2


9  

The JSON specification stipulates that keys and (string) values be quoted with double-quotes.

JSON规范规定了使用双引号引用的键和(字符串)值。

HTML attributes can be enclosed in either single or double quotes.

HTML属性可以包含在单引号或双引号中。

Personally, I wouldn't fight it and just go with what causes the least amount of friction and is easiest for all to understand which in this case is to single quote the HTML attributes and use double-quotes inside the attribute value.

就我个人而言,我不会反对它,我只是选择导致最少摩擦的东西,并且最容易理解在这种情况下哪个是单引号HTML属性,并在属性值中使用双引号。

#3


9  

Try this and you can keep nicely formatted JSON in the attribute:

尝试一下,您可以在属性中保留格式良好的JSON:

$.parseJSON($('.inside').data('dimensions').replace(/'/g,"\""))

#4


4  

Use &quot; instead of " and &apos; instead of '.

使用“;而不是“and &apos”;而不是”。

#5


0  

Another way is to use the function eval or create a new function. You can use either " or ' in your JSON and you don't need to put " around the keys in the JSON object.

另一种方法是使用函数eval或创建一个新函数。您可以在JSON中使用“或”,也可以不需要“在JSON对象中放置”。

let el = document.getElementById('example');
let person = (new Function(`return ${el.dataset.person}`))();
console.log(person);
person.age++;
console.log(person.age);
<div data-person="{name:'Natalie Portman',age:35}" id="example"></div>