google gson解析json示例

        public class User {
            //省略其它
            public String name;
            public int age;
            public String emailAddress;
        }

  

    private void testGson() {
        Gson gson = new Gson();
        String jsonString2 = "{\"name\":\"张三\",\"age\":24}";
        User user = gson.fromJson(jsonString2, User.class);

        System.out.println("hello world:" + user.name);

        String jsonString = "{'header': {'alerts': [{'AlertID': '2', 'TSExpires': null, 'Target': '1', 'Text': 'woot', " +
                "'Type': '1'}, {'AlertID': '3', 'TSExpires': null, 'Target': '1', 'Text': 'woot', 'Type': '1'}], " +
                "'session': '0bc8d0835f93ac3ebbf11560b2c5be9a'}, 'result': '4be26bc400d3c'}";
        Map map = gson.fromJson(jsonString, Map.class);
        System.out.println(map.getClass().toString());
        System.out.println(map);
    }

  

posted @ 2022-08-18 09:28  bonelee  阅读(38)  评论(0编辑  收藏  举报