2

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;

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But now i got an error like "jsonsyntaxexception malformed Json Exception expected EOF at line 2 column 1"
How are you generating this JSON? Is it a feed or are you generating it locally? Make sure there arent any hidden characters.
yes it is having hidden character..thanks. I have one more question. How can get only one value?..like "product.name"

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.