how to parse JSON file with GSON

前端 未结 3 746
名媛妹妹
名媛妹妹 2020-11-29 02:55

I have a very simple JSON with reviews for products, like:

{
  \"reviewerID\": \"A2XVJBSRI3SWDI\", 
  \"asin\": \"0000031887\", 
  \"reviewerName\": \"abigai         


        
3条回答
  •  不知归路
    2020-11-29 03:24

    You have to fetch the whole data in the list and then do the iteration as it is a file and will become inefficient otherwise.

    private static final Type REVIEW_TYPE = new TypeToken>() {
    }.getType();
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader(filename));
    List data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole reviews list
    data.toScreen(); // prints to screen some values
    

提交回复
热议问题