15

How would I escape space characters in a JSON string? Basically my problem is that I've gotten into a situation where the program that reads the string can use HTML tags for formatting, but I need to be able to use these HTML tags without adding more spaces to the string. so things like

<u>text</u>

is fine, for adding underline formatting

but something like

<font size="14">text</font>

is not fine, because the <font> tag with the size attribute adds an extra space to the string. I know, funny criteria, but at this point thats what has happened.

My first speculative solution would be to have some kind of \escape character that JSON can put in between font and size that will solve my "space" problems, something that the HTML will ignore but leave the human readable string in the code without actual spaces.

 ex. <font\&size="14">text</font>

displays as: text

kind of like &nbsp; but better?

any solutions?

3
  • 2
    Why does space need escaping??
    – Orbling
    Commented May 19, 2011 at 22:52
  • Expand for us on why a space needs to be escaped in JSON. As one poster shows, this is possible, but escaping a space does not change the meaning of the JSON, and only increases its size — ie, you are probably doing something wrong.
    – Thanatos
    Commented May 19, 2011 at 22:54
  • the JSON is creating strings in a field of an object. "Object" = { "attribute1": "key", "attribute2": "integer", "attribute3": "string"}, there is a separate platform that parses the JSON objects, attribute 3 happens to be parsed as HTML, so it can use HTML tags to further format the text. But the platform makes assumptions about the number of spaces in the string. Adding certain HTML tags will add spaces to the string. I need a way to add spaces without REALLY adding spaces to the human viewable user experience.
    – CQM
    Commented May 19, 2011 at 23:21

1 Answer 1

28

You can use \u0020 to escape the ' ' character in JSON.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.