1

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 ?

2
  • @FedericoklezCulloca, Can you please show how it should be with one example ? Commented Apr 4, 2018 at 14:26
  • Sorry, wrong language. See the other answers. Commented Apr 4, 2018 at 14:30

3 Answers 3

3

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"
}
Sign up to request clarification or add additional context in comments.

Comments

2

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"))+"\"";

Comments

1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.