android - FastJson 通过TypeReference 解析空数据数据报错
PHP中文网
PHP中文网 2017-04-17 13:02:25
0
3
814
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中文网

认证高级PHP讲师

全部回覆(3)
刘奇

我覺得這個問題你做一下異常處理不行麼? 看這錯誤說的是文法問題:W/System.err(7864): com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 19, fieldName data

巴扎黑

第二種情況回傳空數據,

小葫芦

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板