-1

how to deserialize below string in android. I have tried below

String json= ls.get(j).getCwc();
Example example = new Gson().fromJson(json,Example.class);

Json

[{
  "company":"gjjzh",
  "AvgSal":"hjsj"
},
{
  "company":"hjd",
  "AvgSal":"hjd"
},
{
  "company":"hm",
  "AvgSal":"lk"
},
{
  "company":"er",
  "AvgSal":"io"
},
{
  "company":"uo",
  "AvgSal":"tr"
}]
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
sai android
  • 139
  • 1
  • 12

2 Answers2

0
String json= ls.get(j).getCwc();
Type type = new TypeToken<List<Example>>(){}.getType();
List<Example> example = new Gson().fromJson(json,type);

where Example is

public class Example {

@SerializedName("company")
@Expose
private String company;
@SerializedName("AvgSal")
@Expose
private String avgSal;

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

public String getAvgSal() {
return avgSal;
}

public void setAvgSal(String avgSal) {
this.avgSal = avgSal;
}

}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
0

You'll need to create a model class for the object.

Example.java

public class Example{
    String company = "";
    String AvgSal = "";
}

and then you need to write code as below to convert JSONArray string into List<Model>.

String json= ls.get(j).getCwc();
Type modelListType = new TypeToken<ArrayList<Example>>(){}.getType();
ArrayList<Example> modelList = new Gson().fromJson(json, modelListType);

This will convert JSONArray into ArrayList.

Chintak Patel
  • 748
  • 6
  • 24