解决问题:The type com.fasterxml.jackson.core.JsonProcessingException异常解决
package com.zyw;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @ClassName:Json
* @author zhangyanwei
* @Description:json与Java互转
* @Date:2017年8月23号
*/
public class Json {
public static void main(String[] args) throws ParseException, IOException {
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));
/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
* ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
* writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
* writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
* writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
*/
ObjectMapper mapper = new ObjectMapper();
//User类转JSON
//输出结果:{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合转JSON
//输出结果:[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
}
}
mapper.writeValueAsString(user);这行代码报错:The type com.fasterxml.jackson.core.JsonProcessingException cannot be resolved. It is indirectly referenced from required .class files
解决办法:导入jackson-core版本的jar包即可。
jackson系列jar包下载地址:http://download.csdn.net/download/hengji666/9946624,此资源中有4个关于jackson的包,建议导包的时候全部导入
在Stack Overflow看到的解决方法地址:https://stackoverflow.com/questions/25413842/the-type-com-fasterxml-jackson-core-jsongenerator-cannot-be-resolved-it-is-indi
上述代码在运行中可能会报错:
此时报错的原因:可参照此博客来。http://blog.csdn.net/jamesjxin/article/details/46606307
然后,在浏览的过程中发现,可能是由于自己少导包所致,于是将jackson的剩余annotation和jr-all包一起导入,然后重新运行,无误
项目导jar包可参照此博客:http://blog.csdn.net/mazhaojuan/article/details/21403717