Parsing a single json entry into multiple objects using Gson

I have a json answer that looks like this:

{ "id":"001", "name":"Name", "param_distance":"10", "param_sampling":"2" } 

And I have two classes: Teste and Parameters

 public class Test { private int id; private String name; private Parameters params; } public class Parameters { private double distance; private int sampling; } 

My question is: is there a way to make Gsson understand that some json attributes should go into the Parameters class, or is there the only way to "manually" parse this?

EDIT

Ok, just to make my comment on @MikO more readable:

I will add the object list to the json output, so the json response should look like this:

  { "id":"001", "name":"Name", "param_distance":"10", "param_sampling":"2", "events":[ { "id":"01", "value":"22.5" }, { "id":"02", "value":"31.0" } ] } 

And the Deserializer class will look like this:

 public class TestDeserializer implements JsonDeserializer<Test> { @Override public Test deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); Test test = new Test(); test.setId(obj.get("id").getAsInt()); test.setName(obj.get("name").getAsString()); Parameters params = new Parameters(); params.setDistance(obj.get("param_distance").getAsDouble()); params.setSampling(obj.get("param_sampling").getAsInt()); test.setParameters(params); Gson eventGson = new Gson(); Type eventsType = new TypeToken<List<Event>>(){}.getType(); List<Event> eventList = eventGson.fromJson(obj.get("events"), eventsType); test.setEvents(eventList); return test; } } 

And do:

 GsonBuilder gBuilder = new GsonBuilder(); gBuilder.registerTypeAdapter(Test.class, new TestDeserializer()); Gson gson = gBuilder.create(); Test test = gson.fromJson(reader, Test.class); 

Gives me a test object the way I wanted.

+4
source share
1 answer

How to make Gson understand that you need to write your own deserializer by creating a TypeAdapter for your Test class. You can find the information in the Gson User Guide . This is not entirely manual disassembly, but it is not, because you have to tell Gson how to deal with each JSON value ...

It should be something like this:

 private class TestDeserializer implements JsonDeserializer<Test> { public Test deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); int id = obj.get("id").getAsInt(); String name = obj.get("name").getAsString(); double distance = obj.get("param_distance").getAsDouble(); int sampling = obj.get("param_sampling").getAsInt(); //assuming you have suitable constructors... Test test = new Test(id, name, new Parameters(distance, sampling)); return test; } } 

Then you should register the TypeAdapter with:

 GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Test.class, new TestDeserializer()); 

And finally, you just have to parse your JSON, as usual, with

 gson.fromJson(yourJsonString, Test.class); 

Gson automatically uses your custom deserializer to parse your JSON in your Test class.

+3
source

Source: https://habr.com/ru/post/1491675/


All Articles