package com.sinosoft.config;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
public class Util {
public static void main(String[] args){
UserEntity entity = new UserEntity();
entity.setUserName("aa");
List<UserEntity> list = new ArrayList<>();
list.add(entity);
String redisStr01=JSON.toJSONString(entity);//实体类转json字符串
System.out.println("实 体类转 json字符串:"+redisStr01);
String redisStr02=JSON.toJSONString(list);//list集合转json字符串
System.out.println("list 集合转 json字符串:"+redisStr02);
List<UserEntity> list01 = JSON.parseArray(redisStr02,UserEntity.class);//字符串转实体类
System.out.println("json 字符串转list获取属性:"+list01.get(0).getUserName());
UserEntity user = JSON.parseObject(redisStr01,UserEntity.class);
System.out.println("json字符串转实体类获取属性:"+user.getUserName());
}
}
json和对象,集合的相互转换
最新推荐文章于 2022-01-20 17:34:31 发布