Reputation: 353
I want to place all my Json content in String variable in below format.
String inputJSON = "{
\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"customerStatus\":\"\",
\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";
May I know why am getting error in this regarding to complete double quotes ? I think whole string is completed or covered by double quotes. I want to place my json in this format only, May I know please what am missing or doing wrong ?
Upvotes: 1
Views: 88
Reputation: 2685
Java does not support multi-line String literals (yet). So, each line needs to be a complete String, enclosed in double-quotes, and each line needs to be concatenated using a '+'.
final String inputJSON = "{" +
"\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
"\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
"\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
"\"customerStatus\":\"\"," +
"\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";
Maybe more readable:
final String inputJSON =
"{" +
" \"billingAddress\": {" +
" \"city\":\"\"," +
" \"country\":\"\"," +
" \"postalCode\":\"\"," +
" \"state\":\"\"," +
" \"streetAddress\":\"\"" +
" }," +
" \"shippingAddress\": {" +
" \"city\":\"\"," +
" \"country\":\"\"," +
" \"postalCode\":\"\"," +
" \"state\":\"\"," +
" \"streetAddress\":\"\"" +
" }," +
" \"personAddress\": {" +
" \"city\":\"\"," +
" \"country\":\"\"," +
" \"postalCode\":\"\"," +
" \"state\":\"\"," +
" \"streetAddress\":\"\"" +
" }," +
" \"customerStatus\":\"\"," +
" \"createdDate\": \"" + ((String)globalMap.get("Json.Date")) + "\"" +
"}";
With this format, it was easy to see the closing '}' was missing as well.
Good luck.
Upvotes: 1
Reputation: 44970
Sadly Java doesn't support multi-line String
declaration. You need to use +
to concatenate lines
String inputJSON = "{"
+ "\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"
+ "\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"
+ "\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"
+ "\"customerStatus\":\"\",\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";
Upvotes: 2
Reputation: 9766
You need to add "+"
at the end of every line to join them in a valid way:
These 3 are the same:
String s = "abcdef";
String s = "abc"+"def";
String s = "abc"+
"def";
So for you example:
String s =
"{ "+
"\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
"\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
"\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
"\"customerStatus\":\"\","+
"\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"}";
Also you were missing a }
at the end to have a valid JSON string
If you print out the String above you get (once formatted)
{
"billingAddress": {
"city": "",
"country": "",
"postalCode": "",
"state": "",
"streetAddress": ""
},
"shippingAddress": {
"city": "",
"country": "",
"postalCode": "",
"state": "",
"streetAddress": ""
},
"personAddress": {
"city": "",
"country": "",
"postalCode": "",
"state": "",
"streetAddress": ""
},
"customerStatus": "",
"createdDate": "pop"
}
Upvotes: 3