-1

I have some JSON with the following structure:

{
    "items":[
        {
            "product":{
                "product_id":"some",
                "price_id":"some",
                "price":"some",
                "title_fa":"some",
                "title_en":"Huawei Ascend Y300",
                "img":"some",
                "has_discount_from_price":"0",
                "discount_from_price":null,
                "type_discount_from_price":null,
                "has_discount_from_product":"0",
                "discount_from_product":null,
                "type_discount_from_product":null,
                "has_discount_from_category":"0",
                "discount_from_category":null,
                "type_discount_from_category":null,
                "has_discount_from_brand":"0",
                "discount_from_brand":null,
                "type_discount_from_brand":null,
                "weight":null,
                "features":[
                    {
                        "feature_value":"#000000",
                        "feature_id":"some",
                        "feature_title":"some"
                    },
                    {
                        "feature_value":"some",
                        "feature_id":"1652",
                        "feature_title":"some"
                    }
                ]
            },
            "number":1,
            "feature_id":"56491,56493",
            "price_inf":{
                "has_discount":0,
                "discount_type":0,
                "final_price":"400000",
                "value_discount":0
            },
            "cart_id":13
        }
    ]
}

I'm trying to access the elements "product_id" and "price_id" with the following Java code:

try{
        JSONArray feedArray=response.getJSONArray("items");
        for (int i=0;i<feedArray.length();i++){
            JSONObject feedObj=feedArray.getJSONObject(i);

            JSONObject pro=feedObj.getJSONObject("product");
            Product product = new Product();
            product.setPrice(pro.getDouble("price_id"));
            product.setTitle_fa(pro.getString("price_id"));}}

but i see product not found error.what is wrong in my parser?

hrskrs
  • 4,447
  • 5
  • 38
  • 52
masoud p
  • 503
  • 1
  • 7
  • 15

4 Answers4

1

First of all your JSON is valid. So no worries there. Now regarding your problem, because you haven't posted the logs so I can't tell what the exact problem is. But using this code snippet you can get the desired values.

 try {
      JSONArray itemsJsonArray = jsonObject.getJSONArray("items");
      for (int i = 0; i < itemsJsonArray.length(); i++){
            JSONObject itemJsonObject = itemsJsonArray.getJSONObject(i);
            JSONObject productObject = itemJsonObject.getJSONObject("product");
            String productId = productObject.getString("product_id");
            String priceId = productObject.getString("price_id");
      }

 } catch (JSONException e) {
      e.printStackTrace();
}
priyankvex
  • 5,760
  • 5
  • 28
  • 44
0

Validate and create Pojo for your json here

use

Data data = gson.fromJson(this.json, Data.class);

follow https://stackoverflow.com/a/5314988/5202007

By the way your JSON is invalid .

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0

you are getting a json object from your response not json array you need to make following changes

JSONObject temp =new JSONObject(response);
JSONArray feedArray=temp.getJSONArray("items");
karan
  • 8,637
  • 3
  • 41
  • 78
0

Try converting response string to JSONObject first

try{
JSONObject temp =new JSONObject(responseString); // response is a string
JSONArray feedArray=.getJSONArray("items");
      ....
  }

You may try to use GSON library for parsing a JSON string. Here's an example how to use GSON,

    Gson gson = new Gson(); // Or use new GsonBuilder().create();
 MyType target = new MyType();
 String json = gson.toJson(target); // serializes target to Json
 MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103