public class JSON {
/**
* 实体类转字符串
*/
@Test
public void testObjToJsonstring(){
//方法1
User user=new User();
user.setId(1L);
user.setName("张三");
user.setAge("18");
user.setCreateDate(new Date());
String jsonstring = JSONObject.toJSONString(user);
System.out.println(jsonstring);
//方法2
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
String jsonstring2=gson.toJson(user);
System.out.println(jsonstring2);
}
/**
* JSON转字符串
*/
@Test
public void testJSONToJsonstring(){
JSONObject jsonObject=new JSONObject();
jsonObject.put("id","1L");
jsonObject.put("name","张三");
jsonObject.put("age","16");
System.out.println(JSONObject.toJSONString(jsonObject));
//方法2
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
String jsonstring2=gson.toJson(jsonObject);
System.out.println(jsonstring2);
}
/**
* Map转字符串
*/
@Test
public void testMapToJsonstring(){
Map<String,String> map = new HashMap<String,String>();
map.put("key1","1111");
map.put("key2","2222");
map.put("key3","3333");
map.put("key4","4444");
System.out.println(map.toString());
String jsonObject = JSONObject.toJSONString(map);
System.out.println(jsonObject);
//方法2
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
String jsonstring2=gson.toJson(map);
System.out.println(jsonstring2);
}
/**
* Map转json
*/
@Test
public void testMapToJson(){
Map<String,Object> mapSum = Maps.newHashMap();
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1","1111");
map.put("key2","2222");
map.put("key3","3333");
map.put("key4","4444");
mapSum.put("sum",map);
JSONObject jsonObject=new JSONObject(mapSum);
System.out.println(jsonObject.get("sum"));
}
/**
* 字符串转实体类
*/
@Test
public void testJsonstringToobject(){
String jsonString="{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"}";
User user = JSONObject.parseObject(jsonString, User.class);
System.out.println(user.getAge());
System.out.println(user.getCreateDate());
System.out.println(user.getId());
System.out.println(user.getName());
}
/**
* 字符串转JSON
*/
@Test
public void testJSONStringToJson(){
String jsonString="{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println(jsonObject.get("age"));
System.out.println(jsonObject.get("createDate"));
System.out.println(jsonObject.get("id"));
System.out.println(jsonObject.get("name"));
}
/**
* 字符串转JSONArray
*/
@Test
public void testJSONStringTo字符串转JSONArray(){
String jsonString2="[{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"},{\"age\":\"19\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"李四\"}]";
JSONArray array = JSONArray.parseArray(jsonString2);
System.out.println(array.toJSONString());
List<User> userList = JSONArray.parseArray(jsonString2,User.class);
for (User user : userList) {
System.out.println(user.getName());
}
}
}
fastJson和gson使用,字符串和实体以及map的相互转化
最新推荐文章于 2024-04-21 05:32:00 发布