-3

I am trying to get all the data coming from a API service, but I am having problems accessing the different "levels" inside the JSON response.

I believe my main problem comes when the inner levels include []

I am showing here a reduced part of the JSON response (the entire response is huge). But this part represent clearly my issue

{ 
   “CustomerList”:{ 
      “CustomerType”:”residential”,
      “maxAllowed”:”256”,
      "serverdate":"2017-05-02",
      “Purchases”:[ 
         { 
            “Car”:[ 
               { 
                  “customerName”:”Fredrik”,
                  “price”:”25890”,
                  “currency”:”EUR”,
                  “Item”:{ 
                     “code”:”Audi”,
                     “model”:”A3”,
                     “engine”:”diesel”,
                     “data”:”2017-03-12”,
                     "$":"\n"
                  },
                  "Destination":{ 
                     “country”:”Germany”,
                     “arrivalDate”:”March 25”,
                     "id":"02201403",

I have been able to access CustomerList and Purchases like this

    JSONObject customer = (JSONObject) response.get("CustomerList");
    JSONArray purchases = customer.getJSONArray("Purchases");

However now I am getting troubles accessing inner fields like Car, Item or Destination and their elements.

The following gives me JSONException

for (int i = 0;i< purchases.length();i++){
      JSONObject car = purchases.getJSONObject(i);
      String custName = car.getString("customerName");
codeKiller
  • 5,493
  • 17
  • 60
  • 115
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Mohammed Atif May 02 '17 at 09:13

3 Answers3

1

Car is a JSONArray, and not a JSONObject

for (int i = 0;i< purchases.length();i++){
      JSONArray array = purchases.getJSONArray(i);
      for (int j = 0; j < array.length(); j++){
          // loop through here again to get the JSONObject    
      }
} 

In the inner loop, you get JSONObject and can call
String custName = array.getJSONObject(j).getString("customerName");

If you look at the exception you will probably see something like
you cannot cast a ... to a JSONObject

Denny
  • 1,766
  • 3
  • 17
  • 37
1

In your case, you are getting the wrong object,

Purchases is an JSONArray who contains objects of a supposed class Purchase. And every instance of it has a instance of a JSONArray named Car (who contains Cars).

Given that, the way to access to the first car is :

for (int i = 0;i< purchases.length();i++){
  JSONObject p = purchases.getJSONObject(i);
  JSONObject car = p.getJSONObject(0);
  String custName = car.getString("customerName");

I encourage you strongly to use a JSON serializer like gson to avoid treating with these issues. Take a look to jsonschema2pojo to generate the classes of your hierachy

crgarridos
  • 8,758
  • 3
  • 49
  • 61
  • Thanks for that answer cgarrido, from your link I can see that it is easy to create classes to reference different `Car`, but how to use them in the code?, your first link `gson`is not working – codeKiller May 02 '17 at 11:28
  • I just fixed the link. Gson is really easy to use, look for examples in the net. This could be helpful for you https://www.javacodegeeks.com/2013/08/getting-started-with-google-gson.html. Please, vote up my answer if it helped you – crgarridos May 02 '17 at 14:39
  • yes it helped and actually solved my problem, voting up and accepting!, THANKS. – codeKiller May 03 '17 at 06:28
0

Car is also JSONArray so try like below:

for (int i = 0;i< purchases.length();i++){
          JSONArray carArr = purchases.getJSONArray(i);
           for (int j = 0;j< carArr.length();j++){
           JSONObject car = carArr.getJSONObject(j);
           String custName = car.getString("customerName");
          }
    }
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13