how to parse JSON file with GSON

前端 未结 3 745
名媛妹妹
名媛妹妹 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:31

    In case you need to parse it from a file, I find the best solution to use a HashMap to use it inside your java code for better manipultion.

    Try out this code:

    public HashMap myMethodName() throws FileNotFoundException
    {
        String path = "absolute path to your file";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
    
        Gson gson = new Gson();
        HashMap json = gson.fromJson(bufferedReader, HashMap.class);
        return json;
    }
    

提交回复
热议问题