Reputation: 75645
I want to put <span>
around some numbers in pretty-printed JSON output.
I am using JSON.stringify()
with a replacer to do this. However, as the returned type of an HTML fragment with a span in is a string, these now get quoted.
How can I put spans around numbers?
An example:
JSON.stringify(
{
with_span:1,
without_span:1
},
function(key,value) {
if(key=="with_span")
return '<span>'+value+'</span>'; // but I don't want this quoted! :(
return value;
},
4) // the indent
outputs:
{
"with_span": "1",
"without_span": 1
}
Upvotes: 1
Views: 169
Reputation: 688
It's not possible with JSON.stringify()
.
JSON.stringify()
will return a valid JSON string, and Without the quotes it is not a valid json string.
To achieve what you want, you have to traverse the object yourself and render it as HTML.
I don't know your use case, but maybe you could just use a library to do the work? For example google-code-prettify.
Upvotes: 1