I have a json file formatted as the following:
[{ 'title': 'Java', 'authors': ['Auth', 'Name'] }, { 'title': 'Java2', 'authors': ['Auth2', 'Name2'] }, { 'title': 'Java3', 'authors': ['Auth3', 'Name3'] }]
So, I tried to use the gson library to parse the file with the following code:
JsonElement jelement = new JsonParser().parse(pathFile); JsonObject jObject = jelement.getAsJsonObject(); JsonArray jOb = jObject.getAsJsonArray(""); final String[] jObTE = new String[jOb.size()]; for (int k=0; k<jObTE.length; k++) { final JsonElement jCT = jOb.get(k); JsonObject jOTE = jCT.getAsJsonObject(); JsonArray jContentTime = jOTE.getAsJsonArray("content_time"); final String[] contentTime = new String[jContentTime.size()]; for (int i=0; i<contentTime.length; i++) { final JsonElement jsonCT = jContentTime.get(i); JsonObject jObjectTE = jsonCT.getAsJsonObject(); JsonArray jTE = jObjectTE.getAsJsonArray(""); final String[] contentTimeTE = new String[jTE.size()]; for (int j=0; j<contentTimeTE.length; j++) { final JsonElement jsonCTTE = jTE.get(j); contentTime[j] = jsonCTTE.getAsString(); } } }
But at the same time, I found this error: java.lang.IllegalStateException: Not a JSON Object in the second line.
java json file gson
user2520969
source share