-1

I am consuming a third party API using SpringBoot and Java.Their requirement is to send the JSON in below format.

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

But what i am sending is this

{
  "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"
        }
      }
    }
  ]
 }

Can anyone please tell me how we can generate the json using GSON or any other lib so that i can get '\n' in the json. I have tried to use .toString() method but its not working. I have used .replaceAll("\n","\\n") that also does not work.

@Samabcde What i want is like this 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'

But what actually I am getting by GSON is below:(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"
        }
      }
    }
  ]
 }
rak_6040
  • 65
  • 1
  • 1
  • 6
  • 1
    Your JSON file looks fine, they only added quotes as they show how to send it directly from code. Try printing out their example using System.out.println, and you see that it looks like your example – Ferrybig Jan 16 '20 at 11:29
  • Can you add a example program with the expected string value and current output string value? – samabcde Jan 16 '20 at 12:42
  • @samabcde : First json with "\n" is what i expect . Last one is what is I have generated using GSON and then printing using System.out.println – rak_6040 Jan 16 '20 at 12:53
  • @Ferrybig there is no problem with json at all. I want my json to include "newline" character \n like the first one . If you can help how to do that in java or using GSON – rak_6040 Jan 16 '20 at 12:55
  • [Ferrybig](https://stackoverflow.com/users/1542723/ferrybig) Could you please open this as it is not a question of pretty formatting but of adding escape character into the json – rak_6040 Jan 16 '20 at 13:12

1 Answers1

0

I was able to handle quoted text directly without any additional settings or changes:

import com.google.gson.Gson;

public class GSONTest {

    public String value;

    public static void main(String[] args) {
        Gson g = new Gson();
        GSONTest gt = new GSONTest();
        gt.value = "Test using \"quoted\" strings.";
        System.out.println("String: " + gt.value);
        System.out.println("JSON: " + g.toJson(gt));
    }
}

Output:

String: Test using "quoted" strings.
JSON: {"value":"Test using \"quoted\" strings."}
Redar
  • 83
  • 10
  • [Redar](https://stackoverflow.com/users/4612034/redar)...am looking something like the latest edit one...if you can help – rak_6040 Jan 16 '20 at 14:28