はじめに:ここでは、Alibabaのfastjsonが使用されています(文字通り、高速jsonは、JSONに関連する文字列を提供し、すばやく変換できます)。
依存関係は次のとおりです。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
JSON文字列へのMAP
文字列str = JSON.toJSON(collection).toString();
strは変換された文字列です。
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);
出力結果は次のとおりです。結果は間違いなく問題ありません、biubiu--
信頼性を確認してください:
マップするJSON文字列
マップj2map = JSON.parseObject(json string、Map.class);
j2mapは変換されたマップです
Map j2map = JSON.parseObject(s, Map.class);
System.out.println("后map"+j2map);
System.out.println(j2map.get("key1"));
System.out.println(j2map.get("news"));
結果:
問題がある場合は、指摘してください