目录
-
- 1、Map转JSON
- 2、**Map**转String
- 3、JSON转String
- 4、JSON转Map
- 5、String转JSON
- 6、将Json格式的字符串转换为对象
- 7、将map转换为对象
- 第二种 google
-
1、Map转JSON
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "a");
map.put("b", "123");
JSONObject json = new JSONObject(map);
2、Map转String
Map<String, Object> map = new HashMap<>();
map.put("a", "b");
String s = JSONObject.toJSONString(map);
3、JSON转String
JSONObject json = new JSONObject();
json.put("c", "v");
json.put("z", "123n);
json.toJSONString();
4、JSON转Map
JSONObject json = new JSONObject();
json.put("ccc", "321");
json.put("bbb", "123");
Map<String, Object> map = (Map<String, Object>)json;
5、String转JSON
String str = "{"username":"dsad","qwewqe":"123"}";
JSONObject json = JSONObject.parseObject(str);
6、将Json格式的字符串转换为对象
import lombok.Data;
@Data
public class AlertRule {
private String relation;
private UpperLower diffAbs;
private UpperLower diffPercent;
}
@Data
public class UpperLower {
private BigDecimal upper;
private BigDecimal lower;
}
AlertRule alertRule= JSON.parseObject("{
\"diffAbs\":{
\"upper\":2000,\"lower\":23000},\"diffPercent\":{
\"upper\":2000,\"lower\":23000},\"relation\":\"OR\"}",AlertRule.class);
System.out.println(alertRule.getDiffAbs());
7、将map转换为对象
public class Test01 {
private static class Obj1 {
private String School;
private List<String> studen;
public List<String> getStuden() {
return studen;
}
public void setStuden(List<String> studen) {
this.studen = studen;
}
public String getSchool() {
return School;
}
public void setSchool(String school) {
School = school;
}
}
public static void main(String[] args) {
Map<String,String