【objectMapper实体转换异常】 com.fasterxml.jackson.databind.exc.MismatchedInputException

20 篇文章 1 订阅

 

大家好,我是烤鸭:

    采坑实录,想把json数据直接转成对象,其中有个属性是list<T>:

 

异常 1

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

这个是获取到前端传递的参数:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}";
ObjectMapper objectMapper = new ObjectMapper();
UserInfoRequest userInfoRequest = objectMapper.readValue(str, UserInfoRequest.class);
System.out.println(userInfoRequest);

报上面的错。UserInfoRequest 就不贴了,属性都是能对应上的。其中有个List<phoneInfo>属性。

查了一些资料。改成下面的代码:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ;
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
UserInfoRequest userInfoRequest = objectMapper.readValue(str, UserInfoRequest.class);
System.out.println(userInfoRequest);

就报异常2的错。

 

异常 2 

com.fasterxml.jackson.databind.exc.MismatchedInputException:

Cannot construct instance of `com.xxx.xxx` (although at least one Creator exists):

no String-argument constructor/factory method to deserialize from String value

因为自己测试的时候没有这个问题,就仔细对比了测试的时候str和前端的传值。

错误的:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}";

正确的:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92184\",\"phoneInfo\":[{\"name\":\"张三\",\"mobile\":\"15899996666\"},{\"name\":\"李四\",\"mobile\":\"138556688\"},{\"name\":\"王五\",\"mobile\":\"13855661118\"}],\"time\":\"1540969882\",\"longitude\":\"116.28033\"}";

 关键就在于 phoneInfo 后面跟的是字符串(""包括住的)还是 数组后者list对象。如果是字符串就报这个错了。

\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"1311111019\"}]\"

简单一点的方式:​ 把你的字符串去这个网站看一下是否是标准json。下面两个图一眼就看出问题了。

http://www.bejson.com/ ​

 

总结:

知道问题在哪了,就好改了。因为获取到前端参数的是v=1&x=2&b=3这种形式的。

解析参数的时候,放到map<String,String>中,最后把map转成json字符串的格式。

没有考虑到前端传递的是list这种的格式,判断如果是 [ ]这种的话,就转成jsonArray放到map。

map的格式是 <String,Object>。代码如下:(pheader 是解密后的参数,v=1&x=2&b=3这种形式)

public static String getParam(String cipher)throws Exception{
		Map<String, Object> params = new HashMap<String,Object>();
		//解析参数
		String pheader = "";
		pheader = AESUtil.decrypt(PpsConfig.getString("appkey"), cipher);
		String[] pArr = pheader.split("&");
		for (int i = 0; i < pArr.length; i++) {
			String[] tmp = pArr[i].split("=");
			if (tmp.length == 2) {
				//说明是数组
				if(tmp[1].startsWith("[")){
					JSONArray array = JSONArray.parseArray(tmp[1]);
					params.put(tmp[0], array);
				}else {
					params.put(tmp[0], tmp[1]);
				}
			}else{
				params.put(tmp[0], "");
			}
		}
		return JSONObject.toJSONString(params);
	}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤鸭的世界我们不懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值