1

I know how to get data from normal json data but recently I came across this new question where the json looks like

 "product_1": {
"productId": "product_1",
"customerId": "customer_1",
"brandCode": "BRD0050",
"brandName": "ABC.ST.",
"productCode": 700251,
"productDesc": "ABC  AB 50gm Sac",
"mrp": 15,
"expiry": 1608461977
},
"product_2": {
"productId": "product_2",
"customerId": "customer_1",
"brandCode": "BRD0050",
"brandName": "ABC.ST.",
"productCode": 700251,
"productDesc": "ABC  AB 50gm Sac",
"mrp": 17,
"expiry": 1608462037
},
"product_3": {
"productId": "product_3",
"customerId": "customer_1",
"brandCode": "BRD0050",
"brandName": "ABC.ST.",
"productCode": 700251,
"productDesc": "ABC  AB 50gm Sac",
"mrp": 17,
"expiry": 1608461978
}...

goes till product_150

so I looked for quite some time in google but not able to get any value this is what i have tried

    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        //    JSONArray m_jArry = obj.getJSONArray("product_" + j);
        Iterator<String> keyIterator = obj.keys();
        Log.e("Details",""+obj.keys().hasNext());
        while(keyIterator.hasNext()) {
            String productKey = keyIterator.next();

            JSONObject jo_inside = obj.getJSONObject(productKey);


            ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> m_li;

           /* for (int i = 0; i < jo_inside.length(); i++) {
                Log.e("Details",""+jo_inside.length());
                Log.e("Details-->", jo_inside.getString("productId"));
                String formula_value = jo_inside.getString("formule");
                String url_value = jo_inside.getString("url");

                //Add your values in your `ArrayList` as below:
                m_li = new HashMap<String, String>();
                m_li.put("formule", formula_value);
                m_li.put("url", url_value);

                formList.add(m_li);


            }

            */
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

where I am getting data from the asset file. I have only tried to get data in Log any clue what I am doing wrong or what my approach should be? currently, I made a for loop till 150 and getting the data like that.

2
  • 1
    instead of parsing your json with iterate that json string, you can simply use GSON or another libs for getting better result, take look at this: tutorialspoint.com/gson/index.htm Commented Jan 19, 2020 at 19:31
  • 1
    I have no issue with the any of the method the problem is with the json data its like product_1 ...product_150 @javadroid Commented Jan 19, 2020 at 19:43

1 Answer 1

2

I am guessing that obj.getJSONArray is not returning anything because the JSON is not notated as an array. Remove your outer for loop, then use obj.keys() which should give you an array of keys at the top level of your json object, that based on your provided sample would be ["product_1", "product_2", "product_3"], then you can loop through that array to get each production object, for example;

Iterator<String> keyIterator = obj.keys();
while(keyIterator.hasNext()) 
{
    String productKey = keyIterator.next();
    JSONObject jo_inside = obj.getJSONObject(productKey);

    ... rest of your inner for loop here ...
}
Sign up to request clarification or add additional context in comments.

12 Comments

how you can get the size from obj.size() its not array or list?
Yes, you are right, I edited my answer to use an iterator instead, but the point it is, your JSON doesn't have any array in it, so you need to grab the list of Keys at the top level, and grab the object for each top level key to get your data.
sir what i tried to do is to generate the keys but i guess i was wrong there
You might have been able to achieve it by generating the keys yourself, if you assume that the keys are always and forever going to be in the format of "product_" + [some number], but for each of those keys, you'd still want to skip right to calling obj.getJSONObject(somekey), because there is no array in your JSON to retrieve by calling getJSONArray.
Is there an exception occuring somewhere inside the loop? That would stop it from going to the next iteration. Maybe try wrapping everything inside the while loop inside a try..catch to make sure an inner exception doesn't break the loop?
|

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.