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.