1

I had gone through most of the answers in stack overflow and tried some of the answers which didn't work.

It is a response from a SOAP API.

I tried by using

JSONObject obj = new JSONObject(response);
JSONArray heroArray = new JSONArray();
JSONObject one = obj.getJSONObject("getProjectDetailsResult");
JSONObject two = one.getJSONObject("NewDataSet");
heroArray= two.getJSONArray("Rec");

for (int i = 0; i < heroArray.length(); i++) {
    JSONObject heroObject = heroArray.getJSONObject(i);
    Hero hero = new Hero(heroObject.getString("decProjectID"), 
heroObject.getString("chvProjectNameEng"));

This is what I am getting in the LogCat

2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err: org.json.JSONException: Value {"decProjectID":"100300230049","intProjectSlNo":"49",......"percentage":"0"} at Rec of type org.json.JSONObject cannot be converted to JSONArray
2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:588)

I tried

JSONObject heroArray = new JSONObject();
JSONObject one = obj.getJSONObject("getProjectDetailsResult");
JSONObject two = one.getJSONObject("NewDataSet");
heroArray= two.getJSONObject("Rec");

But I am getting error for the rest of the code. I am testing this with the sample from https://www.simplifiedcoding.net/android-volley-tutorial-fetch-json/

1 Answer 1

1

Rec is not JSONArray, it's JSONObject. Try using

try {
    JSONObject obj = new JSONObject(response);
    JSONObject one = obj.getJSONObject("getProjectDetailsResult");
    JSONObject two = one.getJSONObject("NewDataSet");

    if(two.get("Rec") instanceof JSONArray) {

        JSONArray heroArray = two.getJSONArray("Rec");

        for (int i = 0; i < heroArray.length(); i++) {

            JSONObject heroObject = heroArray.getJSONObject(i);

            Hero hero = new Hero(heroObject.getString("decProjectID"),
                heroObject.getString("intProjectSlNo"),
                heroObject.getString("chvProjectName"),
                heroObject.getString("chvProjectNameEng"),
                heroObject.getString("chrProjCatCode"),
                heroObject.getString("chvEngProjCategory"),
                heroObject.getString("nchvSecType"),
                heroObject.getString("chvEngSecType"),
                heroObject.getString("chvImplOfficerDesg"),
                heroObject.getString("chvImplOfficerDesgEng"),
                heroObject.getString("singleYrAmt"),
                heroObject.getString("TotExp"),
                heroObject.getString("percentage"));

            heroList.add(hero);
        }
    } else {
        JSONObject heroObject = two.getJSONObject("Rec");

        Hero hero = new Hero(heroObject.getString("decProjectID"),
            heroObject.getString("intProjectSlNo"),
            heroObject.getString("chvProjectName"),
            heroObject.getString("chvProjectNameEng"),
            heroObject.getString("chrProjCatCode"),
            heroObject.getString("chvEngProjCategory"),
            heroObject.getString("nchvSecType"),
            heroObject.getString("chvEngSecType"),
            heroObject.getString("chvImplOfficerDesg"),
            heroObject.getString("chvImplOfficerDesgEng"),
            heroObject.getString("singleYrAmt"),
            heroObject.getString("TotExp"),
            heroObject.getString("percentage"));

        heroList.add(hero);
    }
} catch ( Exception ex) {
    ex.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

7 Comments

how do I iterate through the objects?
No need to iterate. It's give u Hero
And the property inside Rec is different. So you have to update your Hero model. Can you post your Hero model?
@JayanDev, have you got it?
I was trying to do it using Iterator
|

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.