0

Part of my JSON file:

[
{"id": 755, "listId": 2, "name": ""},
{"id": 203, "listId": 2, "name": ""},
{"id": 684, "listId": 1, "name": "Item 684"},
{"id": 276, "listId": 1, "name": "Item 276"},
{"id": 736, "listId": 3, "name": null},
{"id": 926, "listId": 4, "name": null},
{"id": 808, "listId": 4, "name": "Item 808"}]

I want to parse the JSON file into Item objects while skipping over the null values. However I'm getting an error now from gson

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 2 column 2 path $[0]

private String deserializeItem() throws IOException, JSONException {
        String itemText = "";
        //Item item = new Item();
        Gson gson = new Gson();

        InputStream is = getAssets().open("JSONFile.json");
        String jsonTxt = IOUtils.toString(is, "UTF-8");
        //Map map = gson.fromJson(jsonTxt, Map.class);
        //Map<String,Item> map = new HashMap<String, Item>();
        //map = (Map<String,Item>) gson.fromJson(jsonTxt, map.getClass());
        //List<Item> itemList = Arrays.asList(gson.fromJson(jsonTxt, Item[].class));
        //Item item = gson.fromJson(jsonTxt, Item.class);
        JsonElement json = gson.fromJson(new FileReader("fetchJSONFile.json"), JsonElement.class);
        String result = gson.toJson(json);

        System.out.println(jsonTxt);
        //JSONObject json = new JSONObject(jsonTxt);
        //String a = json.getString("1000");
        //System.out.println(a);

        return itemText;

Commented out portions are my various attempts to get this to work. It reads the file now without crashing, but does not parse to an object.

Tiana
  • 5
  • 3
  • Please be more specific about what is not working. – shmosel Jun 15 '23 at 23:28
  • 1
    JSONObject expect the string to be the JSON string, not a path to a file. Maybe this link may help https://stackoverflow.com/a/20090084/9520479 – Jhilton Jun 16 '23 at 00:33
  • Tried the stuff in the link, but when it tries to run the inputStream it crashes with an IOException – Tiana Jun 16 '23 at 01:18
  • Okay, figured out that my method being static was screwing with the parsing. That's fixed, but it still won't map onto an object giving me a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 2 column 2 path $[0] error – Tiana Jun 16 '23 at 02:07

1 Answers1

0

Alright I figured out my issues. First, I needed to make my method non-static, which allowed me to read the JSON file through an InputStream. Then I needed to use an array (but not a list... for some reason?) to map all the json data to objects. At the end my code looked like this:

 private String deserializeItem() throws IOException, JSONException {
    String itemText = "";
    Gson gson = new Gson();
    InputStream is = getAssets().open("JSONFile.json");
    String jsonTxt = IOUtils.toString(is, "UTF-8");
    Item[] items = gson.fromJson(jsonTxt, Item[].class);
    List<Item> itemList = new ArrayList<Item>();

    //Eliminates objects with null and empty item names
    for (int i=0; i<items.length; i++)
    {
        if (items[i].getItemName() == null) {continue;}
        else if (items[i].getItemName().equals("")) {continue;}
        else {itemList.add(items[i]);}
    }

This let me parse the JSON file into objects and remove all null and empty values in it.

Tiana
  • 5
  • 3