fastjson处理复杂类型转换以及特性研究

前端之家收集整理的这篇文章主要介绍了fastjson处理复杂类型转换以及特性研究前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//方法一
List<RequestParam> requestParams = JSONArray.parseArray(test,RequestParam.class);
//方法二
requestParams = JSON.par@R_301_401@bject(test,new TypeReference<List<RequestParam>>(RequestParam.class){});
//方法三
 List<Map<String,Object>> list = JSON.par@R_301_401@bject(memcached.get("ACTIVITY_XY_NEW_USER_" + mobile),new TypeReference<List<Map<String,Object>>>() {});
//通常,我们会希望在响应前台的时候,null弄成"",集合为null的弄成[],那么fastjson也为我们听过了便利的特性来达到效果
 HomeFloorBean2 homeFloorBean2 = new HomeFloorBean2();
        homeFloorBean2.setName("test");
        System.out.println("fastjson:" + JSON.toJSONString(homeFloorBean2,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty));

//关于fastjson对象为null的时候,处理变为""
   /**
     * 处理对象为null
     * @param object
     * @param features
     * @return
     */
    private String toJSONString(Object object,SerializerFeature... features) {
        SerializeWriter out = new SerializeWriter();
        String s;
        JSONSerializer serializer = new JSONSerializer(out);
        SerializerFeature arr$[] = features;
        int len$ = arr$.length;
        for (int i$ = 0; i$ < len$; i$++) {
            SerializerFeature feature = arr$[i$];
            serializer.config(feature,true);
        }
        serializer.getValueFilters().add(new ValueFilter() {
            public Object process(Object obj,String s,Object value) {
                if (null != value) {
                    if (value instanceof java.util.Date) {
                        return String.format("%1$tF %1tT",value);
                    }
                    return value;
                } else {
                    return "";
                }
            }
        });
        serializer.write(object);
        s = out.toString();
        out.close();
        return s;
    }
//fastjson和springMVC替换jackson
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        <property name="serializerFeatures">
            <list>
                 <!-- 输出key时是否使用双引号 -->
                        <value>QuoteFieldNames</value>
                        <!-- 是否输出值为null的字段 -->
                        <value>WriteMapNullValue</value>
                        <!-- 数值字段如果为null,输出为0,而非null -->
                        <value>WriteNullNumberAsZero</value>
                        <!-- List字段如果为null,输出为[],而非null -->
                        <value>WriteNullListAsEmpty</value>
                        <!-- 字符类型字段如果为null,输出为"",而非null -->
                        <value>WriteNullStringAsEmpty</value>
                        <!-- Boolean字段如果为null,输出为false,而非null -->
                        <value>WriteNullBooleanAsFalse</value>
                        <!-- null String不输出  -->
                        <value>WriteNullStringAsEmpty</value>
                        <!-- null String也要输出  -->
                        <value>WriteMapNullValue</value>
                        <!-- Date的日期转换器 -->
                        <value>WriteDateUseDateFormat</value>
                        <!-- 将Long类型写为String类型解决js精度丢失问题 -->
                        <value>BrowserCompatible</value>
            </list>
        </property>
    </bean>

 <!-- 会自动注册了validator  ConversionService  -->
    <mvc:annotation-driven>
        <!--enableMatrixVariables="true">-->
        <mvc:message-converters register-defaults="true">
            <!-- StringHttpMessageConverter编码为UTF-8,防止乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
                <property name="supportedMediaTypes">
                    <list>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="text"/>
                            <constructor-arg index="1" value="html"/>
                            <constructor-arg index="2" value="UTF-8"/>
                        </bean>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="*"/>
                            <constructor-arg index="1" value="*"/>
                            <constructor-arg index="2" value="UTF-8"/>
                        </bean>
                    </list>
                </property>
            </bean>

            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"
                  id="fastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>text/json;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <!--序列化选项-->
                <property name="fastJsonConfig" ref="fastJsonConfig"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
原文链接:https://www.f2er.com/json/289288.html

猜你在找的Json相关文章