Patan
Patan

Reputation: 17883

How to deserialize json map of String string

I have json as below

{
    "group": "Mygroup",
    "name1": "aaa",
    "name2": "bbb"

}

I know that we can deserialize to map. But how can we wrap it inside the object, without any custom deserializer

 public class GroupInfo {
    private String group
    private Map<String, String> names; 

  }

Upvotes: 0

Views: 75

Answers (1)

kingkupps
kingkupps

Reputation: 3504

Gson offers this kind of deserialization. You just need to define a class with the same fields as the JSON keys of the JSON being parsed.

Article on using Gson for this usage - https://dzone.com/articles/deserializing-json-java-object Gson repo - https://github.com/google/gson

If you aren't dead set on JSON, I would recommend looking into Protocol Buffers. Protocol Buffers offer this type of functionality out of the box, are language agnostic (to a large extent anyways), and they're usually much faster than JSON.

Protocol Buffers - https://developers.google.com/protocol-buffers/docs/tutorials

Upvotes: 1

Related Questions