当前位置: 代码迷 >> 综合 >> fastjson解析JSON和转换JSON,java String 转Map(TypeReference)
  详细解决方案

fastjson解析JSON和转换JSON,java String 转Map(TypeReference)

热度:53   发布时间:2024-01-17 22:34:43.0

使用fastjson 实现String转List、Map、JSON

直接上代码:

public static void testJSONParseArray() {//造数据Map map = new HashMap();map.put("k11", "v11");map.put("k12", "v12");List list = new ArrayList();Map map1 = new HashMap();map1.put("k21", "v21");map1.put("k22", "v22");list.add(map);list.add(map1);//把list转成JSON形式的StringString listJson = JSONObject.toJSONString(list);System.out.println(listJson);//String转成JSSONArray形式JSONArray jsonArray = JSONArray.parseArray(listJson);System.out.println(jsonArray);//用fastjson把JSON转成ListMap,借助非常好用的TypeReferenceList<Map<String,Object>> param = JSONObject.parseObject(jsonArray.toJSONString(),new TypeReference<List<Map<String,Object>>