0

I am generating this json using GSON in java (CURRENT)

{
  "scheduleAt": "2020-01-16T09:45:29.361Z",
  "serviceType": "TRUCK",
  "stops": [
    {
      "location": {
        "lat": "109.063753",
        "lng": "312.998866"
      },
      "addresses": {
        "en_IN": {
          "displayString": "Please bring the BAG",
          "country": "TH"
        }
      }
    }
  ]
 }

I am expecting below one : (EXPECTED)

{\n \"scheduleAt\": \"2018-12-31T14:30:00.00Z\",\n \"serviceType\": \"MOTORCYCLE\",\n \"requesterContact\": { \"name\": \"Peter Pan\", \"phone\": \"232\" },\n \"stops\": [\n {\n \"location\": { \"lat\": \"-6.255431000000001\", \"lng\": \"106.60114290000001\" },\n \"addresses\": {\n \"en_ID\": {\n \"displayString\":\n \"Jl. Perum Dasana Indah No.SD 3/ 17-18, RT.3/RW.1, Bojong Nangka, Klp. Dua, Tangerang, Banten 15810, Indonesia\",\n \"country\": \"ID\"\n }\n }\n },\n {\n \"location\": { \"lat\": \"-6.404722800000001\", \"lng\": \"106.81902130000003\" },\n \"addresses\": {\n \"en_ID\": {\n \"displayString\": \"Jl. Kartini, Ruko No. 1E, Depok, Pancoran MAS, Kota Depok, Jawa Barat 16431, Indonesia\",\n \"country\": \"ID\"\n }\n }\n }\n ],\n \"deliveries\": [\n {\n \"toStop\": 1,\n \"toContact\": {\n \"name\": \"mm\",\n \"phone\": \"9999999\"\n }\n }\n ]\n}\n'

If we use JSON.stringfy() in javascript we get above json. Can anyone tell me how to achieve this in JAVA

1 Answer 1

1

You can use jackson object mapper to achieve this.

ObjectMapper mapper = new ObjectMapper();
value = mapper.writeValueAsString(obj);

Reference:

https://www.programcreek.com/java-api-examples/?class=com.fasterxml.jackson.databind.ObjectMapper&method=writeValueAsString

https://www.baeldung.com/jackson-object-mapper-tutorial

Sign up to request clarification or add additional context in comments.

1 Comment

Sarvani your solution gives only ` "\" ` but i need "\n"

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.