对象 JSON字符串 Map List Object JSONObject JSONArray 互相转换
创建个实体类
public class UmsPermission implements Serializable {
private Long id;
@ApiModelProperty(value = "父级权限id")
private Long pid;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "权限值")
private String value;
@ApiModelProperty(value = "图标")
private String icon;
@ApiModelProperty(value = "权限类型:0->目录;1->菜单;2->按钮(接口绑定权限)")
private Integer type;
@ApiModelProperty(value = "前端资源路径")
private String uri;
@ApiModelProperty(value = "启用状态;0->禁用;1->启用")
private Integer status;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "排序")
private Integer sort;
private static final long serialVersionUID = 1L;
//省略所有getter及setter方法
}
maven 地址
jar 包, com.alibaba.fastjson 版本的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.31</version>
</dependency>
转换需要用到的包路径
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
import java.util.stream.Collectors;
JSON字符串 转 对象
// jsonStr json字符串
// UmsPermission 实体类对象
UmsPermission umsPermission = JSON.parseObject(jsonStr, UmsPermission.class);
JSON字符串 转 Map
// jsonStr json字符串
// Map class
Map<String,Object> map = JSON.parseObject(jsonStr, Map.class);
JSON字符串 转 JSONObject
// jsonStr json字符串
// JSONObject class
JSONObject jsonObject = JSON.parseObject(jsonStr, JSONObject.class);
JSON字符串 转 List
//listJson 是ListJSON字符串 [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
List<UmsPermission> studentList = JSON.parseArray(listJson, UmsPermission.class);
对象 转 JSON字符串
UmsPermission umsPermission = new UmsPermission();
String jsonStr= JSONObject.toJSONString(umsPermission);
// 返回jsonStr格式 {"id":"1","name":"张三"}
对象 转 Map
// 第一种map
BeanMap testMap = new BeanMap(Object bean);
// 第二种map
Map<String,Object> testMap = new BeanMap(Object bean);
//用法
//需要转换的实体类
UmsPermission umsPermission = new UmsPermission();
Map<String,Object> testMap = new BeanMap(umsPermission);
对象 转 JSONObject
UmsPermission umsPermission = new UmsPermission();
JSONObject jsonStu = (JSONObject) JSONObject.toJSON(umsPermission);
Map 转 JSON字符串
Map Map = new HashMap<>();
String jsonStr= JSONObject.toJSONString(Map);
Map 转 JSONObject
Map<String,Object> map = new HashMap<>();
JSONObject jsonStu = (JSONObject) JSONObject.toJSON(map);
Map 转 对象
//map
Map<String,Object> testMap = new HashMap<>();
//对象
UmsPermission umsPermission2 = new UmsPermission();
//转换
BeanUtils.populate(umsPermission2,testMap);
List 转 JSON字符串
//list 对象
List<UmsPermission> list = new ArrayList<UmsPermission>();
String listJson = JSONObject.toJSONString(list);
//list map
List<Map> list = new ArrayList<Map>();
String listJson = JSONObject.toJSONString(list);
LIst 转 JSONArray
List<UmsPermission> list = new ArrayList();
//2、使用JSONArray
JSONArray array = JSONArray.fromObject(list);
JSONObject 转 JSON字符串
JSONObject jsonObject = new JSONObject();
String string = jsonObject.toJSONString();
JSONObject 转 对象
UmsPermission umsPermission= JSON.toJavaObject(json, UmsPermission.class);
JSONObject 转 Map
JSONObject json = new JSONObject();
Map map = JSON.toJavaObject(json, Map.class);
JSONArray 转 List
JSONArray array = new JSONArray();
List<Map<String,Object> list = JSONArray.toList(array, Map.class);
JSONArray 转 JSON字符串
JSONArray array = new JSONArray();
array.toString()