com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): ha

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

19.04.2017 14:16:10 *INFO * [catalina-exec-640072] MediaWebServer: MediaWebServer.doPost requestJson:[ {"MsgHead":{"version":"1.0","direction":"request","msgType":"POST_VOD_INFO"},"MsgBody":{"userLan":"Zh","cuType":"PC","cuVersion":"v1.0","cuVersionDesc":"thirdPlat","systemVersion":"windows","thirdPlatKey":"6d61a6db-9cdd-4a33-8c64-7bd6449e3d4f","token":"82e7ee30-c769-4395-b94a-17d75fa5ce02","vodName":"1《互联网金融报告》:“金融创新”和“规范发展”成为中国互联网金融两大关键词","vodDesc":"博鳌亚洲论坛2017年年会新闻发布会暨博鳌亚洲论坛学术发布会召开。中国社会科学院金融研究所副所长胡滨发表演讲。
记者:新华社博鳌亚洲论坛前方报道组编辑:曹禹  "}}] (MediaWebServer.java, line 69)
19.04.2017 14:16:10 *ERROR* [catalina-exec-640072] MediaWebServer: HttpServer IOException (MediaWebServer.java, line 155)
com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value
 at [Source:  {"MsgHead":{"version":"1.0","direction":"request","msgType":"POST_VOD_INFO"},"MsgBody":{"userLan":"Zh","cuType":"PC","cuVersion":"v1.0","cuVersionDesc":"thirdPlat","systemVersion":"windows","thirdPlatKey":"6d61a6db-9cdd-4a33-8c64-7bd6449e3d4f","token":"82e7ee30-c769-4395-b94a-17d75fa5ce02","vodName":"1《互联网金融报告》:“金融创新”和“规范发展”成为中国互联网金融两大关键词","vodDesc":"博鳌亚洲论坛2017年年会新闻发布会暨博鳌亚洲论坛学术发布会召开。中国社会科学院金融研究所副所长胡滨发表演讲。
记者:新华社博鳌亚洲论坛前方报道组编辑:曹禹  "}}; line: 1, column: 411]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._throwUnquotedSpace(ParserMinimalBase.java:472)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString2(ReaderBasedJsonParser.java:1613)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString(ReaderBasedJsonParser.java:1585)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.getText(ReaderBasedJsonParser.java:233)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:453)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:586)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:435)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:449)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:311)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)

 

 

 

解决办法,在ObjectMapper配置中增加:

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;  

强大一點,還有其他的:

// 允许整数前导为0,eg:"01"形式
            mapper.configure(Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
            mapper.configure(Feature.ALLOW_COMMENTS, true);
            mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

 

JsonParseUtil

package com.allcam.ryb.ads.core.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author 华
 * @version [版本号, 2015-01-18]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JsonParseUtil
{
    public static final Log LOG = LogFactory.getLog(JsonParseUtil.class);
    
    private static ObjectMapper mapper = new ObjectMapper();
    
    static
    {
        // 允许整数前导为0,eg:"01"形式
        mapper.configure(Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        mapper.configure(Feature.ALLOW_COMMENTS, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }
    
    /**
     * 
     * @param object
     * @return
     */
    public static String obj2Json(Object object)
    {
        try
        {
            if (null == object)
            {
                return "";
            }
            // 如果object为空字符串,不处理则会返回两个双引号
            if (object instanceof String && StringUtils.isBlank(object.toString()))
            {
                return "";
            }
            return mapper.writeValueAsString(object);
        }
        catch (Exception e)
        {
            LOG.error("Exception:Object Convert Json String Error...", e);
        }
        return "";
    }
    
    /**
     * 可将json字符串转成任何复杂对象即对象里可含各种复杂类型:List、Map、Set、Object、Object[]
     * 
     * @param json
     * @param clazz
     * @return
     */
    public static  T json2Obj(String json, Class clazz)
    {
        try
        {
            // 设置时间解析格式
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return mapper.readValue(json, clazz);
        }
        catch (Exception e)
        {
            LOG.error("Exception:Json Convert Object Error...", e);
        }
        return null;
    }
    
    /**
     * 把map转换成实体对象
     * 
     * @param map
     * @param clazz
     * @return
     */
    public static  T map2Obj(Map map, Class clazz)
    {
        return mapper.convertValue(map, clazz);
    }
    
    /**
     * 将json字符串转成复制类型即对象里面含有对象或者List情形
     * 
     * @return 复制类型
     */
    public static  T json2ComplexObj(String json, Class clazz)
    {
        T t = null;
        try
        {
            /**
             * 转换成复杂类型用TypeReference
             */
            t = mapper.readValue(json, new TypeReference()
            {
            });
        }
        catch (Exception e)
        {
            LOG.error("Json Convert Complex Object Error...", e);
        }
        return t;
    }
    
    /**
     * 此方法适合复杂类型的json字符串 ,eg:{"id":0,"userId":null} 若形如["11","22"]的简单数组可直接转成List
     * 
     * @param jsonArr json数组
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  List jsonArr2List(String jsonArr, Class clazz)
    {
        List list = new ArrayList();
        try
        {
            List> map = mapper.readValue(jsonArr, List.class);
            for (Map entry : map)
            {
                T t = map2Obj(entry, clazz);
                list.add(t);
            }
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error...", e);
        }
        return list;
    }
    
    /**
     * 适合简单json数组转List eg:List,List
     * 
     * @param jsonArr ["111","222"],[1, 2]
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  List simpleJson2List(String jsonArr, Class clazz)
    {
        List list = new ArrayList();
        try
        {
            list = mapper.readValue(jsonArr, List.class);
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error...", e);
        }
        return list;
    }
    
    /**
     * 将json字符串转换成Map
     * 
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map json2Map(String json)
    {
        Map map = new LinkedHashMap();
        try
        {
            map = mapper.readValue(json, Map.class);
        }
        catch (Exception e)
        {
            LOG.error("Json Convert Map Error...", e);
        }
        return map;
    }
    
}

 

转载于:https://my.oschina.net/yizhichao/blog/882778

你可能感兴趣的:(com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): ha)