Link to home
Start Free TrialLog in
Avatar of renisenbe
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.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

this :
{"key":"value1  
           value2
           value3"}  
is :
{"key":"value1\n\tvalue2\n\tvalue3"}  
So :
String s = "value1\n\tvalue2\n\tvalue3" ;
Avatar of renisenbe
renisenbe

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:

		char c = 0x000A;
		String s = "value1 " + c + " value2 " + c + " value3";

Open in new window


By the way why do you need new lines in the JSON string?
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
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any reason why the value of my answer was "low"? Both my comments are perfectly valid answers.