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

大家好,我是烤鸭:

    采坑实录,想把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\":\"于金坡\",\"mobile\":\"13102039019\"}]\",\"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\":\"于金坡\",\"mobile\":\"13102039019\"}]\",\"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\":\"于金坡\",\"mobile\":\"13102039019\"}]\",\"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\":\"于金坡\",\"mobile\":\"13102039019\"}]\"

简单一点的方式:​ 把你的字符串去这个网站看一下是否是标准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);
	}

猜你喜欢

转载自blog.csdn.net/Angry_Mills/article/details/83588453