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.