-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?

7
  • your json seems to be invalid Commented Aug 31, 2015 at 11:45
  • debug your code and see if feedArray is coming firstly. @KaranMer it think i was formatting error, {} paranthese missing only Commented Aug 31, 2015 at 11:48
  • Your JSON is invalid because top level element in JSON cannot have any name. It should be a unnamed JSON array [..] or object {..}. Commented Aug 31, 2015 at 11:49
  • can u show us the log file ? and json exception found or something Commented Aug 31, 2015 at 11:53
  • You need to get the product id and price like below Commented Aug 31, 2015 at 11:55

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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 .

2 Comments

Why do you say that the json is invalid?
@hrskrs has validated the json now. please check in edit history.
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");

Comments

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

Comments

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.