Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Json Array resulting in This is not a JSON Array exception

Tags:

java

json

gson

I am trying to parse a Json array which looks like this:

{
  "FoodItemData": [
      {
        "country": "GB",
        "id": "100",
        "name": "Steak and Kidney Pie",
        "description": "Tender cubes of steak, with tender lamb kidney is succulent rich gravy.  Served with a side of mashed potatoes and peas.",
        "category": "Dinner",
        "price": "15.95"
      },
      {
        "country": "GB",
        "id": "101",
        "name": "Toad in the Hole",
        "description": "Plump British Pork sausages backed in a light batter.  Served with mixed vegetables and a brown onion gravy.",
        "category": "Dinner",
        "price": "13.95"
      },
      {
        "country": "GB",
        "id": "102",
        "name": "Ploughman’s Salad",
        "description": "Pork Pie, Pickled Onions, Pickled relish Stilton and Cheddar cheeses and crusty French bread.",
        "category": "Lunch",
        "price": "10.95"
      }
]
}

I am using Gson to parse this Json.

URL url = getClass().getClassLoader().getResource("FoodItemData.json");

        FileReader fileReader = null;
        try {
            fileReader = new FileReader(url.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        JsonReader jsonReader = new JsonReader(fileReader);
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray Jarray = parser.parse(jsonReader).getAsJsonArray();

        List<FoodItem> listOfFoodItems = new ArrayList<FoodItem>();

        for (JsonElement obj : Jarray) {
            FoodItem foodItem = gson.fromJson(obj, FoodItem.class);
            listOfFoodItems.add(foodItem);
        }

This code results in java.lang.IllegalStateException: This is not a JSON Array exception. The FoodItem class contains the variables with the same name as in the Json.

public class FoodItem {
    private String id;
    private String name;
    private String description;
    private String category;
    private String price;
    private String country;
}

Am I missing anything here? I tried using the following code as given in this answer, but I got the same exception as the one mentioned in that question. Any help would be appreciated.

Gson gson = new GsonBuilder().create();
Type collectionType = new TypeToken<List<FoodItem>>(){}.getType(); 
List<FoodItem> foodItems = gson.fromJson(jsonReader, collectionType);
like image 955
Rajath Avatar asked Dec 20 '22 22:12

Rajath


1 Answers

You will need to change this

JsonArray jarray = parser.parse(jsonReader).getAsJsonArray();

to

JsonArray jarray = (JsonArray) parser.parse(jsonReader).getAsJsonObject().get("FoodItemData");

Your JSON contains a JSON object at the root called FoodItemData. That element contains the JSON array you are trying to map to a List<FoodItem>

Alternatively, you could create a class that has a field called FoodItemData that is a List<FoodItem>

public class RootElement {
    List<FoodItem> FoodItemData;
    // the good stuff
}

And parse like so

RootElement root = gson.fromJson(jsonReader, RootElement.class);
System.out.println(root.FoodItemData);

Also, note that Java convention states that variable names should start with a lower case character.

like image 153
Sotirios Delimanolis Avatar answered May 21 '23 01:05

Sotirios Delimanolis