一、
- 字符串数组转换成LIst
方式一
//import com.alibaba.fastjson.JSONArray;
String string = (String) request.getSession().getAttribute("test");
//string 的值为 ["72","73","74","75","79"]
List<String> strLislt = JSONArray.parseArray(string,String.class);
//strList的值为 [72, 73, 74, 75, 79]
方式二
//import com.alibaba.fastjson.JSON;
String string = (String)request.getSession().getAttribute("test");
List<String> strLislt = JSON.parseArray(string ,String.class);
2.字符串转换成Map
//import java.util.Map;
//import com.alibaba.fastjson.JSON;
//string 的值为 {"Message":"OK","Code":"OK"}
Map<String, Object> map = (Map)JSON.parse(string);
3.String转换成JSONObject
//import com.alibaba.fastjson.JSON;
//string 的值为 {"Message":"OK","Code":"OK"}
JSONObject obj = JSON.parseObject(string);
String string = obj.getString("Message");
//string 的值为 OK
4.String转换成实例Object
//import com.alibaba.fastjson.JSON;
//string 的值为 {"Message":"OK","Code":"OK"}
User user = JSON.parseObject(string,User.Class);
4.Object 转换成String
//import com.alibaba.fastjson.JSON;
List<String> strList = new ArrayList<>();
strList.add("72");
strList.add("73");
String str = JSON.toJSONString(strList );
//str 的值为["72","73"]
5.JSONObject 转成JSONString
JSONObject jo = new JSONObject();
jo.put("code", "1234");
request.setTemplateParam(jo.toJSONString());
request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"1234\"}");