0

When trying to process JSON, I recive Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

This JSON is a client with array in data but i want to use the same data class for other for example employee, but I need that data can store different classes

{
    "data": [
        {
            "client": "PAL",
            "organization": "*",
            "organization_info": {
                "name": "*",
                "searchKey": "0",
                "id": "0"
            },
            "id": "*****",
            "searchKey": "AJ23",
            "commercialName": "MANOLO",
            "fiscalName": "MANOLO",
            "locations": [{
                    "address": "NUEVA",
                    "city": "SEVILLA",
                    "zipCode": "00000",
                    "country": "ES"}]
        },
        {
            "client": "LAC",
            "organization": "*",
            "organization_info": {
                "name": "*",
                "searchKey": "0",
                "id": "0"
            },
            "id": "*****",
            "searchKey": "0ASD13",
            "commercialName": "DANI",
            "fiscalName": "DANI",
            "locations": [{
                    "address": "FONTANA",
                    "city": "VALLADOLID",
                    "zipCode": "00000",
                    "country": "ES"
                }]
        }]
}

This JSON has a data object since it only has one result

{
    "data": {
        "client": "PAL",
            "organization": "*",
            "organization_info": {
                "name": "*",
                "searchKey": "0",
                "id": "0"
            },
            "id": "*****",
            "searchKey": "AJ2",
            "commercialName": "MANOLO",
            "fiscalName": "MANOLO",
            "locations": [
                    "address": "NUEVA",
                    "city": "SEVILLA",
                    "zipCode": "00000",
                    "country": "ES"
             }]
       }
}

The idea of the Data class is that it be generic to be able to call different classes Customer, Provider, etc. from the main class.

public class Data<T> {
    @SerializedName("data")
    private final List<T> data;
    String links;

    public Data(List<T> data) {
        super();
        this.data = data;
    }

    public List<T> getData() {
        return data;
    }

    //get and set method
public class Client {
    private String client;
    private String organization;
    private Org_info organization_info;
    private String id;
    private String fiscalName;
    private String searchKey;
    private String commercialName;
    private String phone;
    private String email;
    private ArrayList<Locations> locations;

    public Cliente(Org_info organization_info, String client, String organization, String id, String fiscalName, String searchKey,String phone, String email,
            String commercialName,ArrayList<Locations> locations) {
        super();
        this.organization_info=organization_info;
        this.client = client;
        this.organization = organization;
        this.id = id;
        this.fiscalName = fiscalName;
        this.searchKey = searchKey;
        this.commercialName = commercialName;
        this.locations = locations;
    }
    //get and set method

How can I make the data class generic, and be able to store JSON data with the data node from both clients and providers, while receiving an array of data or a data object?

public static void main(String[] args){
    conection();
    InputStreamReader inputStreamReader = new InputStreamReader(http.getInputStream(), "UTF-8");
    BufferedReader br = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = br.read()) != -1) {
        sb.append((char) cp);
    }
    String output = sb.toString();

    final Type tpEnvClient = new TypeToken<Data<Client>>(){}.getType();
    final Data<Client> envEmp = new Gson().fromJson(output, tpEnvClient);

When trying to store an array I get an error because I can't store a list of clients.

4
  • According to the duplicate, declaring private final T data; as private final List<T> data; should do the trick. Commented Dec 29, 2022 at 10:27
  • i change but i have this problem now ` Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $` , now i use ArrayList<Data> la = new ArrayList<Data>(); List<Client> list = new Gson().fromJson(output, la.getClass()); Commented Dec 29, 2022 at 10:48
  • Take a look at this answer stackoverflow.com/questions/24830699/… Commented Dec 29, 2022 at 11:11
  • Does this answer your question? Make GSON accept single objects where it expects arrays Commented Jan 9, 2023 at 0:09

0

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.