Preface: Alibaba's fastjson is used here (literally, fast json, which provides strings related to JSON, which can be quickly converted).
The dependencies are as follows:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
MAP to JSON string
String str=JSON.toJSON(collection).toString();
str is the converted string.
Map<String,Object> map=new HashMap<String,Object>();
map.put("key1","value1");
News news1=new News("1","今天天气不错","天气晴朗","213",new Date(),1L);
News news2=new News("1","今天天气不错","天气晴朗","213",new Date(),1L);
News news3=new News("1","今天天气不错","天气晴朗","213",new Date(),1L);
List<News> news=new ArrayList<>();
news.add(news1);
news.add(news2);
news.add(news3);
map.put("news",news);
String str = JSON.toJSON(map).toString();//JSON是jar包提供的工具类 import com.alibaba.fastjson.JSON;
System.out.println(str);
The output result is: the result is definitely no problem, biubiu--
verify authenticity:
JSON string to Map
Map j2map = JSON.parseObject(json string, Map.class);
j2map is the converted Map
Map j2map = JSON.parseObject(s, Map.class);
System.out.println("后map"+j2map);
System.out.println(j2map.get("key1"));
System.out.println(j2map.get("news"));
Result:
If there is a problem, please point out