fastJson (ObjectMapper) 和 Gson 对象的初步使用呢(一)

首先,两者都是json字符串的转换工具,只不过是性能上、使用上不同

首先使用fastJson
        //定义一个对象Student student = new Student("张三","123456","男",20);//创建一个objectMapper 对象,需要导入依赖包  就是这个包 codehaus.jacksonObjectMapper objectMapper = new ObjectMapper();//将对象转换成json字符串形式String s = objectMapper.writeValueAsString(student);//输出结果:{"username":"张三","password":"123456","gender":"男","age":20}System.out.println(s);//将json字符串转换到map集合中Map map = objectMapper.readValue(s, Map.class);//输出的是一个个键值对形式for (Map.Entry entry : map.entrySet()) {System.out.println(entry.getKey());System.out.println(entry.getValue());System.out.println("---------------------分割线-----------------------");}//将json字符串再转换成student对象Student stu = objectMapper.readValue(s, Student.class);//Student{username='张三', password='123456', gender='男', age=20}System.out.println(stu);
Gson的使用(建议使用)
Student student = new Student("李四","123456","女",18);//使用gson对象将student对象转换成json字符串String gson_stu = new Gson().toJson(student);//{"username":"李四","password":"123456","gender":"女","age":18}System.out.println(gson_stu);//将json字符串转换成对象的形式Student stu = new Gson().fromJson(gson_stu, Student.class);//Student{username='李四', password='123456', gender='女', age=18}System.out.println(stu);//将json转换成map形式Map map = new Gson().fromJson(gson_stu, Map.class);for (Map.Entry entry : map.entrySet()) {System.out.println(entry.getKey() + "==========>" + entry.getValue());}//这种尝试是不可以的/* List list = new Gson().fromJson(gson_stu, List.class);for (Object o : list) {o.toString();}*///定义一个集合,集合中添加多个引用类型的成员List list = new ArrayList();list.add(student);list.add(new Student("王五","123344","男",20));list.add(new Student("赵柳","22344545","男",223));//将对象转换成json字符串格式String gson_list = new Gson().toJson(list);//[{"username":"李四","password":"123456","gender":"女","age":18},{"username":"王五","password":"123344","gender":"男","age":20},{"username":"赵柳","password":"22344545","gender":"男","age":223}]System.out.println(gson_list);//将该对象转换成list集合中 =====>这样做是不行的
//        List list_gson = new Gson().fromJson(gson_list, List.class);
//        for (Student student1 : list_gson) {
//            System.out.println(student1);
//        }//这样也是不行的
//        Student student1 = new Gson().fromJson(gson_list, Student.class);
//        System.out.println(student1);//        Map> fromJson = new Gson().fromJson(gson_list, Map.class);
//        for (Map.Entry> entry : fromJson.entrySet()) {
//            System.out.println(entry.getKey() + "===========>" + entry.getValue());
//        }//将json转换成list集合  ========》成功List list1 = new Gson().fromJson(gson_list, new TypeToken>() {}.getType());for (Student student1 : list1) {System.out.println(student1);}//定义一个Map集合,将map集合转换成json字符串Map map1 = new HashMap();map1.put("111","zhangsan");map1.put("222","lisi");String s = new Gson().toJson(map1);//{"111":"zhangsan","222":"lisi"}System.out.println(s);//再将json字符串转换成map对象Map map2 = new Gson().fromJson(s, Map.class);//{111=zhangsan, 222=lisi}System.out.println(map2);//如果map中存放着student对象Map map3 = new HashMap();map3.put("狗子",new Student("zhangsna","111","男",20));map3.put("二蛋",new Student("lisi","222","女",22));map3.put("瓜皮",new Student("wangwu","333","男",23));String s1 = new Gson().toJson(map3);//{"二蛋":{"username":"lisi","password":"222","gender":"女","age":22},"狗子":{"username":"zhangsna","password":"111","gender":"男","age":20},"瓜皮":{"username":"wangwu","password":"333","gender":"男","age":23}}System.out.println(s1);//成功Map map4 = new Gson().fromJson(s1, Map.class);for (Map.Entry entry : map4.entrySet()) {System.out.println(entry.getKey() + "============>" + entry.getValue());}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部