renisenbe
asked on
Need newlines in json output from Java String
Desired JSON output:
{"key":"value1
value2
value3"}
In Java,
String s = "value1 \\n value2 \\n value3"
but the output is just {"key":"value1 \\n value2 \\n value3"}
I have tried \n, \\n, \\\n, \\\\n and none worked.
{"key":"value1
value2
value3"}
In Java,
String s = "value1 \\n value2 \\n value3"
but the output is just {"key":"value1 \\n value2 \\n value3"}
I have tried \n, \\n, \\\n, \\\\n and none worked.
ASKER
The Json output just prints out the \n\t with the rest of the output. I guess having a newline inside the json value is not a good idea.
Try this:
By the way why do you need new lines in the JSON string?
char c = 0x000A;
String s = "value1 " + c + " value2 " + c + " value3";
By the way why do you need new lines in the JSON string?
ASKER
Because my value1 string is really long. I'm thinking, i might need to make value1, value2 and value3 be elements in an array, not sure how.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Any reason why the value of my answer was "low"? Both my comments are perfectly valid answers.
{"key":"value1
value2
value3"}
is :
{"key":"value1\n\tvalue2\n\tvalue3"}
So :
String s = "value1\n\tvalue2\n\tvalue3" ;