Im using GSON to parse this JSON file:
{
"database" : {
"source" : "google",
"items" : [
{"title" : "hello world",
"id" : "id_hello_world"},
{"title" : "goodbye world",
"id" : "id_goodbye_world"}
]
}
}
Which ive read into String jsonLine
Im trying to parse it and output all the values but im getting a ClassCastException
at the
JsonObject source = database.getAsJsonObject("source")
line.
I think im searching for the data wrong. Im using this to search and output:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = (JsonObject) jelement;
JsonObject database = jobject.getAsJsonObject("database"); //Get Database
JsonObject source = database.getAsJsonObject("source"); //Get Source
System.out.println("Source: " + source.toString()); //Print source
JsonArray items = database.getAsJsonArray("items"); //Get items array
for(int i=0; i< items.size(); i++){ //for every item
JsonObject item = (JsonObject) items.get(i); //Select item i
JsonObject title = (JsonObject) item.getAsJsonObject("title");
JsonObject id = (JsonObject) item.getAsJsonObject("id");
System.out.println("Item " + i + " title : " + title.toString() + ", id : " + id.toString());
}
If someone could correct my code that would be perfect. I know there are other simpler ways to do this using GSON.fromJSON(jsonLine, Wrapper.class)
but im trying to learn to do it this way too. Thanks for the help!
Before you are going to use Gson
method, create your template structure:
class Item{
private String item = "";
private String id = ""; // for sure you can use 'int'
}
public class DataBase{
private List<Item> items = null;
private String source = "";
}
Now your main class:
public class YourClass{
private DataBase database = "";
....
database = gson.fromJson(yourString, YourClass.class);
...
}
Don't forget try/catch
wrapper
Enjoy
use JsonPrimitive
instead of JsonObject
. (it must be internal bug of GSON, because, trying to convert JsonPrimitive
to JsonObject
was impossible, as far as the version of GSON 2.2.3) So, your code comes like this:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = (JsonObject) jelement;
JsonObject database = jobject.getAsJsonObject("database"); // Get
// Database
JsonPrimitive source = database.getAsJsonPrimitive("source"); // Get
// Source
System.out.println("Source: " + source.toString()); // Print source
JsonArray items = database.getAsJsonArray("items"); // Get items array
for (int i = 0; i < items.size(); i++) { // for every item
JsonObject item = (JsonObject) items.get(i); // Select item i
JsonPrimitive title = item.getAsJsonPrimitive("title");
JsonPrimitive id = item.getAsJsonPrimitive("id");
System.out.println("Item " + i + " title : " + title.toString()
+ ", id : " + id.toString());
}
As Maxim Shoustin showed, creating template will be good solution for it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.