json转换成对象

一.gson


com.google.code.gson
gson
2.2.4

//1、创建Gson对象  

Gson gson = new Gson();  

User user = new User(1, "liujinghan", "123456", "[email protected]"); 

 //2、调用toJson(Object)将对象转为字符串  String json = gson.toJson(user); 

 //{"id":1,"username":"liujinghan","password":"123456","email":"[email protected]"} 

 System.out.println(json);  

//3、将json字符串封装为java对象[json字符串 的属性名要和javabean的属性一样]  

User user2 = gson.fromJson(json, User.class); 

 System.out.println(user2);

二.JSONObject

  例如:

UnZip unZip = (UnZip) JSONObject.toBean(JSONObject.fromObject(string.toString()),UnZip.class);

 

转载于:https://www.cnblogs.com/cuiguangpeng/p/11347466.html

你可能感兴趣的:(json转换成对象)