Reputation: 200
I have the following JSON assigned directly to a JS variable:
{
"name": "NaHCO3",
"parent": "Top Level",
"_children": [
{
...
},
]
},
With the name attribute, I want it to be NaHCO3, not NaHCO3. How do I achieve this?
I tried looking for a HTML entity for this, but it seems that there isn't one.
Upvotes: 1
Views: 4329
Reputation: 75645
JSON is not markup language. So it does not care what are the meaning of the values it carries. Somstimes you can use UTF entities as there's tons of various symbols in UTF (like subscript three you look for):
{
"name": "NaHCO₃",
...
},
But if you want certain markup features in general , you must choose the way to achieve it, and then format the value stored to use it. I.e. you can use HTML tags or markdown etc.
Upvotes: 1
Reputation: 5556
Simply use the char ₃
or \u2083
.
\u2083
is what PHP uses to encode into a JSON:
$mol_encoded = json_encode(html_entity_decode('NaHCO₃'));
$mol_decoded = json_decode($mol_encoded);
echo "$mol_encoded\n$mol_decoded";
returns:
"NaHCO\u2083"
NaHCO₃
and in JS JSON.parse('"NaHCO\u2083"')
returns "NaHCO₃"
Upvotes: 5