1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class TestJSON { @Test public void testJSON() throws IOException { Per p=new Per("Alice",23); Gson gson = new Gson(); String json = gson.toJson(p); System.out.println(json); File file=new File("E:\\CSOURCE\\Kaikeba\\lib\\test\\test\\test1.json"); PrintStream ps=new PrintStream(file); ps.println(json); ps.close(); } class Per{ String name="NULL"; int age=-1; public Per(String name, int age) { this.name = name; this.age = age; } public Per() { } @Override public String toString() { return "Per{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|