1

What is the best way from the given JSON to generate List of SimpleTestClass type where there's a new SimpleTestClass object for the values in the recipients array in the JSON with code set as well.

public class SimpleTestClass{
     String code;
     String recipient; 
}

JSON payload:

{
     "code": 123,
     "recipients": [
        "888888",
        "222222"
     ]
}

2 Answers 2

2

If JSON structure does not fit to POJO model you need to write custom deserialiser or create a new POJO model which fits JSON and after deserialisation process convert it to required model. Below you can find solution with custom deserialiser which allow you to handle given JSON in a very flexible way:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.JsonAdapter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class GsonApp {

    public static void main(String[] args) {
        String json = "{\"code\": 123,\"recipients\": [\"888888\",\"222222\"]}";

        Gson gson = new GsonBuilder().create();

        List<Recipient> recipients = gson.fromJson(json, Recipients.class).getRecipients();
        recipients.forEach(System.out::println);
    }
}

class RecipientsJsonDeserializer implements JsonDeserializer<Recipients> {

    @Override
    public Recipients deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
        List<Recipient> recipients = new ArrayList<>();

        JsonObject root = json.getAsJsonObject();
        String code = root.get("code").getAsString();
        JsonArray recipientsArray = root.getAsJsonArray("recipients");
        recipientsArray.forEach(item -> {
            recipients.add(new Recipient(code, item.getAsString()));
        });

        return new Recipients(recipients);
    }
}

@JsonAdapter(RecipientsJsonDeserializer.class)
class Recipients {

    private final List<Recipient> recipients;

    public Recipients(List<Recipient> recipients) {
        this.recipients = recipients;
    }

    // getters, toString
}

class Recipient {

    private final String code;
    private final String recipient;

    public Recipient(String code, String recipient) {
        this.code = code;
        this.recipient = recipient;
    }

    // getters, toString
}

Above code prints:

Recipient{code='123', recipient='888888'}
Recipient{code='123', recipient='222222'}
Sign up to request clarification or add additional context in comments.

Comments

0
class SimpleTestClass {
    String code;
    List<String> recipients;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<String> getRecipients() {
        return recipients;
    }

    public void setRecipients(List<String> recipients) {
        this.recipients = recipients;
    }

}

public class ServerMain {
    public static void main(String[] args) {

        Gson g = new Gson();
        SimpleTestClass class = g.fromJson(json, SimpleTestClass.class);

    }
}

3 Comments

Yes. B ut what if want to have exactly the structure: for every recipients new SimpleTestClass object with given code ?
Can you explain it more? I am not able to get you.
For example in json in recipients array we have two values "88888" and "22222". I want to find the best way to generate two object of type SimpleTestClass, one for "88888" and one for "22222" with setted code which in this situation is "123". I just search for most optimal way of doing this, maybe something with Gson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.