Reputation: 303
Below is my code. While i am running it shows "json syntax exception expected a string but was begin_object". I don't know why it shows the error.
{
"products": [
{
"name": "gam",
"pplsft": "75665",
"imei": "Ptwm ",
"created_at": "2012-12-03 04:58:01"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 05:44:01"
},
{
"name": "gptw",
"pplsft": "0",
"imei": "at",
"created_at": "2012-12-03 05:58:18"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 23:32:06"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 23:35:25"
}
]
}
and the class file are, but i dont know exactly how to create the class file for json parsing using gson. Can anobdy explain this??
public class Results {
public String name;
@SerializedName("pplsft")
public int pplsft;
@SerializedName("imei")
public String imei;
@SerializedName("created_at")
public int created_at;
}
public class SearchResponse {
@SerializedName("products")
public List<Result> products;
@SerializedName("name")
public String name;
@SerializedName("pplsft")
public int pplsft;
@SerializedName("imei")
public String imei;
@SerializedName("created_at")
public int created_at;
public List<Result> getProducts() {
return products;
}
public void setProducts(List<Result> products) {
this.products = products;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPplsft() {
return pplsft;
}
public void setPplsft(int pplsft) {
this.pplsft = pplsft;
}
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
public int getCreated_at() {
return created_at;
}
public void setCreated_at(int created_at) {
this.created_at = created_at;
}
}
This is the main method for calling the data from the json.
response = gson.fromJson(reader, SearchResponse.class);
Toast.makeText(this,response.name, Toast.LENGTH_SHORT).show();
List<Result> list = response.products;
Upvotes: 2
Views: 2175
Reputation: 1459
Your object should be as follows for JSON:
SearchResponse response = gson.fromJson(reader, SearchResponse.class);
Then to get your list:
List<Product> mProducts = response.products;
To go through your list you do the following:
for( Product pro : mProducts ){
String pName = pro.name;
......
}
or you can just do it manually(get name from the first object;
mProducts.get(0).name;
Now your class:
public class SearchResponse {
@SerializedName("products")
public List<Product> products;
public class Product {
@SerializedName("name")
public String name;
@SerializedName("pplsft")
public String pplsft;
@SerializedName("imei")
public String imei;
@SerializedName("created_at")
public String created_at;
}
}
your JSON
{
"products":[
{
"name":"gam",
"pplsft":"75665",
"imei":"Ptwm ",
"created_at":"2012-12-03 04:58:01"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 05:44:01"
},
{
"name":"gptw",
"pplsft":"0",
"imei":"at",
"created_at":"2012-12-03 05:58:18"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 23:32:06"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 23:35:25"
}
]
}
Perhaps the Solution to this Post could be of some help to you as well.
Upvotes: 3