検索

ホームページ  >  に質問  >  本文

android - FastJson 通过TypeReference 解析空数据数据报错

01-21 10:38:37.413: W/System.err(7864): com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 19, fieldName data
01-21 10:38:37.414: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:232)
01-21 10:38:37.414: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:135)
01-21 10:38:37.414: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:33)
01-21 10:38:37.414: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:420)
01-21 10:38:37.415: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:330)
01-21 10:38:37.415: W/System.err(7864):     at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:135)
01-21 10:38:37.415: W/System.err(7864):     at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:551)
01-21 10:38:37.415: W/System.err(7864):     at com.alibaba.fastjson.JSONReader.readObject(JSONReader.java:189)
01-21 10:38:37.416: W/System.err(7864):     at com.alibaba.fastjson.JSONReader.readObject(JSONReader.java:184)


    public int code;

    public String msg;

    public T data;

服务器返回的数据为空的情况下报错即data="",但是如果不为空则没有问题

TypeReference<Response<User>> reference = new TypeReference<Response<User>>() {
            };
PHP中文网PHP中文网2772日前816

全員に返信(3)返信します

  • 怪我咯

    怪我咯2017-04-17 13:04:25

    我觉得这个问题你做一下异常处理不行么? 看这错误说的是语法问题:W/System.err(7864): com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 19, fieldName data

    返事
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:04:25

    第二种情况返回空数据,

    返事
    0
  • 高洛峰

    高洛峰2017-04-17 13:04:25

    FastJSON可以通过修改配置进行空字符串过滤
    用了Spring的话可以在xml里配置

    <bean id="fastJsonHttpMessageConverter"
                      class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    
                    <property name="supportedMediaTypes">
                        <list>
                            <!--解决IE JSON提示下载的问题-->
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
    
                    <property name="features">
                        <array>
                            <!--NULL过滤-->
                            <value>QuoteFieldNames</value>
                            <value>WriteMapNullValue</value>
                            <value>WriteNullStringAsEmpty</value>
                            <value>WriteNullListAsEmpty</value>
                            <value>WriteNullNumberAsZero</value>
                            <value>WriteNullBooleanAsFalse</value>
    
                            <!--浏览器兼容-->
                            <value>BrowserCompatible</value>
                            <!--斜杠处理-->
                            <value>WriteSlashAsSpecial</value>
                        </array>
                    </property>
                </bean>
    

    没有Spring的话在JSON.toJSONString(SerializerFeature.WriteMapNullValue)....... ),填入相应的参数
    具体请看com.alibaba.fastjson.serializer

    public enum SerializerFeature {
        QuoteFieldNames,
        UseSingleQuotes,
        WriteMapNullValue,
        WriteEnumUsingToString,
        UseISO8601DateFormat,
        WriteNullListAsEmpty,
        WriteNullStringAsEmpty,
        WriteNullNumberAsZero,
        WriteNullBooleanAsFalse,
        SkipTransientField,
        SortField,
        /** @deprecated */
        @Deprecated
        WriteTabAsSpecial,
        PrettyFormat,
        WriteClassName,
        DisableCircularReferenceDetect,
        WriteSlashAsSpecial,
        BrowserCompatible,
        WriteDateUseDateFormat,
        NotWriteRootClassName,
        DisableCheckSpecialChar,
        BeanToArray,
        WriteNonStringKeyAsString,
        NotWriteDefaultValue;
    
        ......... 
    }
    

    作用就是变量名,比如同时配置了 WriteMapNullValueWriteNullStringAsEmpty,当容器里是NULL时JSON转化成""
    参考:
    https://github.com/alibaba/fastjson/wiki

    返事
    0
  • キャンセル返事