I have the following method that when receiving an xml in a String converts it to objects of type CitiMarketSSAEvent
public CitiMarketSSAEvent convertXmlToObject(String xml){
CitiMarketSSAEvent citiMarket = null;
JAXBContext jaxbContext = JAXBContext.newInstance(CitiMarketSSAEvent.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
citiMarket = (CitiMarketSSAEvent) unmarshaller.unmarshal(reader);
return citiMarket;
}
and then I have the following method that converts the objects of that class into a json
public String convertObjectToJson(CitiMarketSSAEvent citiMarketObject) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.disableHtmlEscaping();
Gson gson = gsonBuilder.create();
return gson.toJson(citiMarketObject, CitiMarketSSAEvent.class);
}
and it paints the json as follows
{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
but I want to add the "event" key to the json in such a way that I get something like this:
{
"event":{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
}
and then I modified my method as follows using JSONObject
public String convertObjectToJson(CitiMarketSSAEvent citiMarketObject) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.disableHtmlEscaping();
JSONObject jsonObj = new JSONObject();
jsonObj.put("event",citiMarketObject);
Gson gson = gsonBuilder.create();
return gson.toJson(jsonObj, JSONObject.class);
}
and it throws me the json but the key "nameValuePairs" was added, how can it be removed? Or how else can I do it to just add the "event" key?
{
"nameValuePairs":{
"event":{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
}
}
2
Answers
Try this code, it is supposed to do what you need.
Use Map instead of JSONObject would fix nameValuePairs
The code should be like this