FastJson生成jsonObject时null字段不显示的处理方法
微wx笑
2019-07-04【库&框架】
774
14
0关键字:
FastJson
初次使用FastJson的小伙伴,可能遇到这样一种情况,生成的JSON字符串少了很多属性,一查发现缺少的属性有一个共同特征,就是值都是Null。
FastJson生成jsonObject时null字段不显示的处理方法
初次使用FastJson的小伙伴,可能遇到这样一种情况,生成的JSON字符串少了很多属性,一查发现缺少的属性有一个共同特征,就是值都是Null。
解决方法:
使用String str =JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue);
如果是注入的时候呢?参考以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ //创建FastJson信息转换对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //创建Fastjosn对象并设定序列化规则 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue); // 中文乱码解决方案 List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //设定json格式且编码为UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //规则赋予转换对象 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); } |
通过 FastJsonConfig 的 setSerializerFeature 方法,可以设置很多序列化规则。
具体的通过以下源码可以了解到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /* * Copyright 1999-2018 Alibaba Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.fastjson.serializer; /** * @author wenshao[szujobs@hotmail.com] */ public enum SerializerFeature { QuoteFieldNames, /** * */ UseSingleQuotes, /** * */ WriteMapNullValue, /** * 用枚举toString()值输出 */ WriteEnumUsingToString, /** * 用枚举name()输出 */ WriteEnumUsingName, /** * */ UseISO8601DateFormat, /** * @since 1.1 */ WriteNullListAsEmpty, /** * @since 1.1 */ WriteNullStringAsEmpty, /** * @since 1.1 */ WriteNullNumberAsZero, /** * @since 1.1 */ WriteNullBooleanAsFalse, /** * @since 1.1 */ SkipTransientField, /** * @since 1.1 */ SortField, /** * @since 1.1.1 */ @Deprecated WriteTabAsSpecial, /** * @since 1.1.2 */ PrettyFormat, /** * @since 1.1.2 */ WriteClassName, /** * @since 1.1.6 */ DisableCircularReferenceDetect, // 32768 /** * @since 1.1.9 */ WriteSlashAsSpecial, /** * @since 1.1.10 */ BrowserCompatible, /** * @since 1.1.14 */ WriteDateUseDateFormat, /** * @since 1.1.15 */ NotWriteRootClassName, /** * @since 1.1.19 * @deprecated */ DisableCheckSpecialChar, /** * @since 1.1.35 */ BeanToArray, /** * @since 1.1.37 */ WriteNonStringKeyAsString, /** * @since 1.1.42 */ NotWriteDefaultValue, /** * @since 1.2.6 */ BrowserSecure, /** * @since 1.2.7 */ IgnoreNonFieldGetter, /** * @since 1.2.9 */ WriteNonStringValueAsString, /** * @since 1.2.11 */ IgnoreErrorGetter, /** * @since 1.2.16 */ WriteBigDecimalAsPlain, /** * @since 1.2.27 */ MapSortField; SerializerFeature(){ mask = ( 1 << ordinal()); } public final int mask; public final int getMask() { return mask; } public static boolean isEnabled( int features, SerializerFeature feature) { return (features & feature.mask) != 0 ; } public static boolean isEnabled( int features, int fieaturesB, SerializerFeature feature) { int mask = feature.mask; return (features & mask) != 0 || (fieaturesB & mask) != 0 ; } public static int config( int features, SerializerFeature feature, boolean state) { if (state) { features |= feature.mask; } else { features &= ~feature.mask; } return features; } public static int of(SerializerFeature[] features) { if (features == null ) { return 0 ; } int value = 0 ; for (SerializerFeature feature: features) { value |= feature.mask; } return value; } public final static SerializerFeature[] EMPTY = new SerializerFeature[ 0 ]; public static final int WRITE_MAP_NULL_FEATURES = WriteMapNullValue.getMask() | WriteNullBooleanAsFalse.getMask() | WriteNullListAsEmpty.getMask() | WriteNullNumberAsZero.getMask() | WriteNullStringAsEmpty.getMask() ; } |
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lib-frame/2019-07-04/24.html
上一篇:返回列表