1. ホーム
  2. json

JsonParseException: 不正な引用符なしの文字 ((CTRL-CHAR, コード 10)): ha

2022-02-07 05:30:01

2017.04.19 14:16:10 *INFO * [catalina-exec-640072] MediaWebServer: MediaWebServer.doPost requestJson:[ {"MsgHead":{"version ":"1. 0","action":"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 The Internet Finance Report. "金融革新"と"標準化発展"は中国のインターネット金融の二つのキーワードになる"vodDesc":"博愛アジアフォーラム2017年次総会記者発表と博愛アジアフォーラム学術大会が開催された。中国社会科学院金融研究所の胡斌副所長が講演を行いました。
記者:新華社通信 ボアオフォーラム アジア前方取材班 編集部 曹佑 "}}] をご覧ください。(MediaWebServer.java, 行 69)
2017.04.19 14:16:10 *ERROR* [catalina-exec-640072] MediaWebServer: HttpServer IOException (MediaWebServer.java, line 155) です。
com.fasterxml.jackson.core.JsonParseException: 不正な引用符なしの文字 ((CTRL-CHAR, code 10)): 文字列値に含めるには、バックスラッシュを使用してエスケープする必要があります。
 at [出典 {"MsgHead":{"version":"1. 0","direction":"request","msgType": "POST_VOD_INFO"},"MsgBody".{"MsgBody"{"MsgHead":"version":"1: {"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インターネット金融レポート:"金融イノベーション"と"標準化発展"が2大キーワードになる","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)
    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;
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;

/**
 * <One sentence feature brief> <Feature detailed description>
 * 
 * @author Hua
 * @version [version number, 2015-01-18]
 * @see [related classes/methods]
 * @since [product/module version]
 */
public class JsonParseUtil
{
    public static final Log LOG = LogFactory.getLog(JsonParseUtil.class);
    
    private static ObjectMapper mapper = new ObjectMapper();
    
    static
    {
        // allow integer leading to 0,eg:"01" form
        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 "";
            }
            // If object is an empty string, it will return two double quotes if it is not handled
            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 "";
    }
    
    /**
     * Can convert json strings to any complex objects, i.e. objects with various complex types: List, Map, Set, Object, Object[]
     * 
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T json2Obj(String json, Class<T> clazz)
    {
        try
        {
            // Set the time parsing format
            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;
    }
    
    /**
     * Convert map to entity object
     * 
     * @param map
     * @param clazz
     * @return
     */
    public static <T> T map2Obj(Map<String, Object> map, Class<T> clazz)
    {
        return mapper.convertValue(map, clazz);
    }
    
    /**
     * Convert json string to copy type i.e. object containing object or List<T> case inside
     * 
     * @return copy type
     */
    public static <T> T json2ComplexObj(String json, Class<T> clazz)
    {
        T t = null;
        try
        {
            /**
             * Convert to complex types with TypeReference
             */
            t = mapper.readValue(json, new TypeReference<T>()
            {
            });
        }
        catch (Exception e)
        {
            LOG.error("Json Convert Complex Object Error... ", e);
        }
        return t;
    }
    
    /**
     * This method is suitable for complex types of json strings ,eg:{"id":0,"userId":null} If a simple array in the shape of ["11","22"] can be directly converted to List
     * 
     * @param jsonArr json array
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> jsonArr2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            List<Map<String, Object>> map = mapper.readValue(jsonArr, List.class);
            for (Map<String, Object> entry : map)
            {
                T t = map2Obj(entry, clazz);
                list.add(t);
            }
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error... ", e);
        }
        return list;
    }
    
    /**
     * Suitable for simple json arrays to List eg:List<String>,List<Integer>
     * 
     * @param jsonArr ["111","222"],[1, 2]
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> simpleJson2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            list = mapper.readValue(jsonArr, List.class);
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error... ", e);
        }
        return list;
    }
    
    /**
     * Convert json string to Map
     * 
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> json2Map(String json)
    {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        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