Java 类型转换String,List,Map,Array

时间:2022-03-28 19:33:06

1. JsonString转为Map

 String jsoNStr = "{\n" +
                "\t\"TestArray\": [\"1\", \"2\", \"3\"]\n" +
                "}";
 Map map = JSON.parseObject(jsoNStr,Map.class);

2.Object转为JsonArray(得保证obj是个Array数组,否则报错)

Object obj = map.get("TestArray");
JSONArray myJsonArray = JSONArray.fromObject(obj);

3.JsonArray转为List

List<String> arr = (List) myJsonArray;

4.List转为String[]

String[] arr2 = arr.toArray(new String[arr.size()]);