Reputation: 75
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
Upvotes: 0
Views: 780
Reputation: 69
You can use jackson object mapper to achieve this.
ObjectMapper mapper = new ObjectMapper();
value = mapper.writeValueAsString(obj);
Reference:
https://www.baeldung.com/jackson-object-mapper-tutorial
Upvotes: 1