0

I'm trying to deserialize a java object to JSON object with the code below and I recognized that the parameters which start with upper case has been written double. For example;

Request req = new Request();
req.setMAC("abcdef");
req.setMACParams("term:id:orderno");
req.setOrderNo("999xdef123");

final ObjectMapper mapper = new ObjectMapper(); 

String json = mapper.writeValueAsString(req);

Inside of json string:

{ "MAC":"abcdef","MACParams":"term:id:orderno","OrderNo":"999xdef123","mac":"abcdef","macparams":"term:id:orderno","orderno":"999xdef123" }

So what should i do to prevent this double code?

Lino
  • 19,604
  • 6
  • 47
  • 65
okduman
  • 9
  • 1
  • 3
  • hmm java is case sensitive, `MAC` is different from `mac`... not sure what the question is... – nafas Aug 17 '17 at 08:23
  • https://stackoverflow.com/a/37686018/1927832 – Suresh Atta Aug 17 '17 at 08:23
  • Post a complete minimal example reproducing the problem. We cant see your code if you don't post it. And the few lines you posted isn't even valid Java code. – JB Nizet Aug 17 '17 at 08:44
  • I'm not allowed to post confidential code so that I put an example code. I think my question is clear to understand and doesn't matter wheter it is valid or not because it's so simple but i fixed it. – okduman Aug 17 '17 at 09:04

2 Answers2

1

This problem occurs due to upper case letters used in field property names. Just use @JsonProperty annotation in each field of Request class then it will de-serialize to given name only.

E.g.:

class Request{
    @JsonProperty("MAC")
    private String mac;

}
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
0

Do you have getter & setter in your class Request, but those getter does not follow java bean rules, The field "MAC" whith getMAC and setMAC whill be correct , but "getMac" will generate mac twice in the result.

xiaoming
  • 1,069
  • 8
  • 10