Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert the Null to an empty string when fastjson generates json

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to achieve fastjson to generate json Null into an empty string", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert the Null to an empty string when fastjson generates json.

For instance

Generate JSON code snippet

Map

< String , Object >

JsonMap = new HashMap

< String , Object>

(); jsonMap.put ("a", 1); jsonMap.put ("b", "); jsonMap.put (" c ", null); jsonMap.put (" d "," wuzhuti.cn "); String str = JSONObject.toJSONString (jsonMap); System.out.println (str); / / output result: {" a ": 1," b ":", d: "wuzhuti.cn"}

From the output, we can see that the key corresponding to null has been filtered out; this is obviously not what we want, so we need to use the SerializerFeature serialization genus of fastjson.

This is the method: JSONObject.toJSONString (Object object, SerializerFeature...) Features)

Whether Fastjson's SerializerFeature serialization attribute QuoteFieldNames---- uses double quotes when outputting key. Default is whether true WriteMapNullValue--- outputs fields with a value of null. Default is false WriteNullNumberAsZero-- numeric field if null, output is 0, and non-null WriteNullListAsEmpty--List field is null, output is [], and non-null WriteNullStringAsEmpty- character type field is null, output is "", instead of null WriteNullBooleanAsFalse-Boolean field, if null, output is false, not nullMap.

< String , Object >

JsonMap = new HashMap

< String , Object>

JsonMap.put ("a", 1); jsonMap.put ("b", "); jsonMap.put (" c ", null); jsonMap.put (" d "," wuzhuti.cn "); String str = JSONObject.toJSONString (jsonMap,SerializerFeature.WriteMapNullValue); System.out.println (str) / / output result: {"a": 1, "b": "," c ": null," d ":" wuzhuti.cn "} so far, I believe you have a deeper understanding of" how to realize how to convert Null to an empty string when fastjson generates json ". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report