JAVA中json的转换
首先需要导入alibaba的json包
pom:
com.alibaba
fastjson
1.2.47
举例如下:
- Map转JSON
Map map = new HashMap();
map.put("a", "a");
map.put("b", "123");
JSONObject json = new JSONObject(map);
- map转string
Map map = new HashMap<>();
map.put("a", "b");
String s = JSONObject.toJSONString(map);
- JSON转String
JSONObject json = new JSONObject();
json.put("c", "v");
json.put("z", "123n);
json.toJSONString();
- JSON转Map
JSONObject json = new JSONObject();
json.put("ccc", "321");
json.put("bbb", "123");
Map map = (Map)json;
- String转JSON
String str = "{\"username\":\"dsad\",\"qwewqe\":\"123\"}";
JSONObject json = JSONObject.parseObject(str);
- String 转换为 实体类对象
String resultStr = "123,456"
JSON.parseObject(resultStr, ResultLoginInfo.class);
腊八节!冲鸭