1

I am getting an json array from rest webservice in response which is like

 [{
"mutualFund":{"fundCode":"XYZ","fundName": "Funds - Global Income 
 Fund (G)-SGD","isin":"LU0882574725","sedol":"1234"}},

 {"brokers":{"fundID":"abcd","fundName":"Funds - Focus 
  Fund A-USD","isin":"LU0197229882","sedol":"6543"}
 }]

I am trying to iterate over all mutualFund arrays attributes to fetch their value. I have tried this code snippet but its returning error --"mutualFund do not exist". In my json file some objects are of mutualfund type and some are of other type with different attributes so I had to iterate and differentiate between both of them . So I cant use getJSONObject(i).

 JSONArray jsonArray=new JSONArray(response.getBody());
  for(int i=0;i<jsonArray.length();i++){
  JSONObject jsonObject=jsonArray.getJSONObject("mutualFund");
  }
1
  • Which json library are you using? Commented Apr 25, 2017 at 16:26

1 Answer 1

2

Based on the classes and methods you're using, I'm assuming you use org.primefaces.json classes. But even if you're using a different API, the logic will be basically the same.

First of all, look at your JSON structure:

[
  {
    "mutualFund": {
      "fundCode": "XYZ",
      "fundName": "Funds-GlobalIncomeFund(G)-SGD","isin":"LU0882574725","sedol":"1234"
     }
  },
  {
    "brokers": {
      "fundID": "abcd",
      "fundName": "Funds-FocusFundA-USD","isin":"LU0197229882","sedol":"6543"
    }
  }
]

It's an array with 2 elements. The first element is an object with just one key (mutualFund) and its value (another object with fundCode and fundName keys). Note that the object has a mutualFund key, and you're trying to get it as if the object itself was a mutualFund. That's what causes the error.

So, to get all mutualFund objects, you need to check every element in the array, and for each element you must check if it has a mutualFund key. Then your code will be like this:

for (int i = 0; i < jsonArray.length(); i++) {
    // get object i
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    // check if object has mutualFund key
    if (jsonObject.has("mutualFund")) {
        // get mutualFund object and do something with it
        JSONObject mutualFund = jsonObject.getJSONObject("mutualFund");
        // do something with mutualFund object (you can get values for fundCode and fundName keys, etc)
    }
}

Note: if you're using a different JSON API, the methods names might differ (instead of has, some uses containsKey, or get(key) != null, but the logic to find mutualFund objects will be the same).

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

2 Comments

I am using org.json lib .
For org.json the code would be the same. Have you tested?

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.