Gson Streaming example

阅读更多

将object序列化到Json文件中

        try {
            Gson gson = new Gson();
            FileOutputStream outputStream = new FileOutputStream("bean.json");
            outputStream.write(gson.toJson(bean).getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 

 

反序列化为Java Object

 

        try {
            Gson gson = new Gson();
            BufferedReader reader = new BufferedReader(new FileReader("bean.json"));
            bean = gson.fromJson(new JsonReader(reader), TestBean.class);
            System.out.println(bean.getLogin().id);
        } catch (IOException e) {
            e.printStackTrace();
        }

 

你可能感兴趣的:(gson)