JAVA对象转JSON字符串
public static void beanTojson() {
Data data = new Data();
data.setAction("add");
data.setId("1");
data.setOrdinal(8L);
data.setOrganUnitFullName("testJSON");
data.setParent("0");
data.setSuborderNo("58961");
String s = JSON.toJSONString(data);
System.out.println("toJsonString()方法:s=" + s);
}
JSON字符串转JSON对象
public static void jsonToJsonBean() {
String s ="{\"action\":\"add\",\"id\":\"1\",\"ordinal\":8,\"organUnitFullName\":\"testJSON\",\"parent\":\"0\",\"suborderNo\":\"58961\"}";
JSONObject jsonObject = JSON.parseObject(s);
String action = jsonObject.getString("action");
String id = jsonObject.getString("id");
System.out.println("action ="+action);
System.out.println("id ="+id);
System.out.println("jsonObject ="+jsonObject);
}
JSON字符串转JAVA对象
public static void jsonStrToJavaBean() {
String s ="{\"action\":\"add\",\"id\":\"1\",\"ordinal\":8,\"organUnitFullName\":\"testJSON\",\"parent\":\"0\",\"suborderNo\":\"58961\"}";
Data data = JSON.parseObject(s, Data.class);
System.out.println("data对象"+data.toString());
System.out.println("action="+data.getAction()+"---id="+data.getId());
Data dd = JSON.parseObject(s, new TypeReference<Data>() {});
System.out.println("另一种方式获取data对象"+dd.toString());
System.out.println("另一种方式获取="+dd.getAction()+"---id="+dd.getId());
}
JAVA对象转JSON对象
public static void jsonBenToJsonObject() {
Data data = new Data();
data.setAction("add");
data.setId("1");
data.setOrdinal(8L);
data.setOrganUnitFullName("testJSON");
data.setParent("0");
data.setSuborderNo("58961");
JSONObject jsonObj = (JSONObject) JSON.toJSON(data);
JSON json = (JSON) JSON.toJSON(data);
System.out.println("jsonObj"+jsonObj);
System.out.println("json对象"+json);
}
JSON字符串 数组类型与JAVA对象的转换
public static void jsonStrToJSONArray() {
String str = "{\"errors\":[{\"code\":\"UUM70004\",\"message\":\"组织单元名称不能为空\",\"data\":{\"id\":\"254\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部门\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"组织单元全称\"},\"success\":false},{\"code\":\"UUM70004\",\"message\":\"组织单元名称不能为空\",\"data\":{\"id\":\"255\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部门\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"组织单元全称\"},\"success\":false}]}";
JSONObject jsonObject = JSON.parseObject(str);
JSONArray error = jsonObject.getJSONArray("errors");
List<Error> errors = JSON.parseObject(error.toJSONString(), new TypeReference<List<Error>>() {
});
for (Error e: errors) {
System.out.println("Error属性="+e.getSuccess());
System.out.println("Error属性="+e.getCode());
System.out.println("Error属性="+e.getMessage());
List<Data> datas = e.getData();
for (Data d: datas) {
System.out.println("data对象属性="+d.getId());
System.out.println("data对象属性="+d.getAction());
System.out.println("data对象属性="+d.getSuborderNo());
}
}
}
Map
将map转换成jsonObject
JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(itemMap));
将jsonObject转换成Map
Map<String, Object> itemMap = JSONObject.toJavaObject(itemJSONObj, Map.class);
Java遍历Map的4种方式
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 2);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
for (Integer key : map.keySet()) {
System.out.println("key = " + key);
}
for (Integer value : map.values()) {
System.out.println("key = " + value);
}
Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
}