标签:color uda java eva image value ima write sys
添加依赖:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.7</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version>2.5.0</version> </dependency>
下面直接上代码:
public void toJosn() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); //json // java 对象 -> json 字符串 // json 字符串 -> java 对象 News news = new News("kudankuaidaole", "放假通知"); String s = objectMapper.writeValueAsString(news); System.out.println("对象转字符串:"+s); System.out.println("============="); List<News> newsList = Arrays.asList( new News("yuandan", "放假"), new News("春节", "快来了") ); //第一种方式 String newslistStr = objectMapper.writeValueAsString(newsList); News[] newsArray = objectMapper.readValue(newslistStr, News[].class); List<News> newsList1 = objectMapper.readValue(newslistStr, new TypeReference<List<News>>() { }); System.out.println(); System.out.println("字符串转对象:"+newsList1); //第二种方式 List<News> newsList2 = new Gson().fromJson(newslistStr, new TypeToken<List<News>>() {}.getType()); //使用gson System.out.println("字符串转对象:"+newsList2); }
结果:
标签:color uda java eva image value ima write sys
原文地址:https://www.cnblogs.com/nongzihong/p/10187234.html