JSON格式互相轉換

1、建立demo用到的實體類

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方法
}
複製代碼

2、maven地址(須要用到的jar包)

<dependency>
        <groupId>com.alibaba</groupId> 
        <artifactId>fastjson</artifactId> 
        <version>1.1.31</version> 
    </dependency>
複製代碼

3、轉換須要用到的包路徑

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;
複製代碼

4、JSON字符串轉對象

// jsonStr json字符串 
    // UmsPermission 實體類對象
    UmsPermission umsPermission = JSON.parseObject(jsonStr, UmsPermission.class);
複製代碼

5、JSON字符串轉MAP

// jsonStr json字符串
     Map class Map<String,Object> map = JSON.parseObject(jsonStr, Map.class);
複製代碼

6、JSON字符串轉JSONObject

// jsonStr json字符串
     JSONObject class JSONObject jsonObject = JSON.parseObject(jsonStr, JSONObject.class);
複製代碼

7、JSON字符串轉List

//listJson 是ListJSON字符串 [{"id":"1","name":"張三"},{"id":"2","name":"李四"}]

    List<UmsPermission> studentList = JSON.parseArray(listJson, UmsPermission.class);
複製代碼

8、對象轉JSON字符串

UmsPermission umsPermission  = new UmsPermission();
    String jsonStr= JSONObject.toJSONString(umsPermission);
     // 返回jsonStr格式 {"id":"1","name":"張三"}
複製代碼

9、對象轉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);
複製代碼

10、對象轉JSonObject

UmsPermission umsPermission = new UmsPermission(); 
    JSONObject jsonStu = (JSONObject)JSONObject.toJSON(umsPermission);
複製代碼

11、Map轉Json字符串

Map Map = new HashMap<>(); 
    String jsonStr= JSONObject.toJSONString(Map);
複製代碼

12、Map轉JsonObject

Map<String,Object> map = new HashMap<>(); 
    JSONObject jsonStu = (JSONObject)JSONObject.toJSON(map);
複製代碼

十3、Map轉對象

//map
    Map<String,Object> testMap = new HashMap<>();
    //對象
    UmsPermission umsPermission2 = new UmsPermission();
    //轉換
    BeanUtils.populate(umsPermission2,testMap);
複製代碼

十4、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);
複製代碼

十5、List轉JsonArray

List<UmsPermission> list = new ArrayList();
    //二、使用JSONArray JSONArray array = JSONArray.fromObject(list);
複製代碼

十6、JsonObject轉Json字符串

JSONObject jsonObject = new JSONObject(); 
    String string = jsonObject.toJSONString();
複製代碼

十7、JsonObject轉對象

UmsPermission umsPermission= JSON.toJavaObject(json, UmsPermission.class);
複製代碼

十8、JsonObject轉Map

JSONObject json = new JSONObject(); 
    Map map = JSON.toJavaObject(json, Map.class);
複製代碼

十9、JsonObject轉List

JSONArray array = new JSONArray();
    List<Map<String,Object> list = JSONArray.toList(array, Map.class);
複製代碼

二10、JsonArray轉Json字符串

JSONArray array = new JSONArray(); 
    array.toString()
複製代碼
相關文章
相關標籤/搜索