利用JSONObject.toJSONString()包含或排除指定的屬性
JSONObject.toJSONString包含或排除指定的屬性
將一個(gè)實(shí)體對(duì)象轉(zhuǎn)換成Json字符串 JSON.toJSONString()
FastJson提供的SerializeFilter類(lèi)可以指定轉(zhuǎn)換時(shí)要包含的屬性,或者指定轉(zhuǎn)換時(shí)要排除的屬性。
JSONObject.toJSONString()默認(rèn)忽略值為null的屬性.
使用JSONObject提供的以下方法將實(shí)體對(duì)象轉(zhuǎn)換成Json字符串:(JSONObject 提供的toJSONString 源碼 自己還沒(méi)看)
public static final String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toString(); } finally { out.close(); } }
演示程序
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.spring.PropertyPreFilters; /** * 使用FastJson將實(shí)體對(duì)象轉(zhuǎn)換成Json字符串測(cè)試類(lèi) */ public class FastJsonApplication { public static void main(String[] args) { User user = new User(); user.setId(1L); user.setUsername("張三"); user.setPassword(""); user.setMobile(null); user.setCountry("中國(guó)"); user.setCity("武漢"); String jsonUser = null; /** * 指定排除屬性過(guò)濾器和包含屬性過(guò)濾器 * 指定排除屬性過(guò)濾器:轉(zhuǎn)換成JSON字符串時(shí),排除哪些屬性 * 指定包含屬性過(guò)濾器:轉(zhuǎn)換成JSON字符串時(shí),包含哪些屬性 */ String[] excludeProperties = {"country", "city"}; String[] includeProperties = {"id", "username", "mobile"}; PropertyPreFilters filters = new PropertyPreFilters(); PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter(); excludefilter.addExcludes(excludeProperties); PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter(); includefilter.addIncludes(includeProperties); /** * 情況一:默認(rèn)忽略值為null的屬性 */ jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat); System.out.println("情況一:\n" + jsonUser); /** * 情況二:包含值為null的屬性 */ jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況二:\n" + jsonUser); /** * 情況三:默認(rèn)忽略值為null的屬性,但是排除country和city這兩個(gè)屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat); System.out.println("情況三:\n" + jsonUser); /** * 情況四:包含值為null的屬性,但是排除country和city這兩個(gè)屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況四:\n" + jsonUser); /** * 情況五:默認(rèn)忽略值為null的屬性,但是包含id、username和mobile這三個(gè)屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat); System.out.println("情況五:\n" + jsonUser); /** * 情況六:包含值為null的屬性,但是包含id、username和mobile這三個(gè)屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況六:\n" + jsonUser); } /** * 用戶實(shí)體類(lèi) */ public static class User { private Long id; private String username; private String password; private String mobile; private String country; private String city; //此處省略了相應(yīng)屬性的set、get方法 }
運(yùn)行結(jié)果:
結(jié)果說(shuō)明:
- 情況一和情況二說(shuō)明了public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature… features)這個(gè)方法將實(shí)體對(duì)象轉(zhuǎn)換成JSON字符串時(shí),默認(rèn)是忽略掉值為null的屬性,并且說(shuō)明了如何使得轉(zhuǎn)換后的JSON字符串包含值為null的屬性。
- 情況三和情況四說(shuō)明了如何使用SerializeFilter來(lái)排除指定屬性,使得轉(zhuǎn)換后的JSON字符串中不包含這些屬性。
- 情況五和情況六說(shuō)明了如何使用SerializeFilter來(lái)包含指定屬性,使得轉(zhuǎn)換后的JSON字符串中只包含這些屬性。
JSONObject toJSONString 遇到的坑
引入pom文件
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> <scope>test</scope> </dependency>
使用JSONObject 輸出 int類(lèi)型的map
是非json格式
import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSONObject; public class JsonTest { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "aaasa"); map.put(2, "bbbbb"); map.put(3, "ccccc"); map.put(4, "ddddd"); map.put(5, "eeeee"); System.out.println(JSONObject.toJSONString(map)); } }
輸出結(jié)果
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- String轉(zhuǎn)JSONObject的兩種方式
- Java中如何將String轉(zhuǎn)JSONObject
- 關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問(wèn)題
- 使用JSONObject.toJSONString 過(guò)濾掉值為空的key
- JSONObject?toJSONString錯(cuò)誤的解決
- 解決JSONObject.toJSONString()輸出null的問(wèn)題
- Java使用fastjson對(duì)String、JSONObject、JSONArray相互轉(zhuǎn)換
- 詳解Java中String JSONObject JSONArray List<實(shí)體類(lèi)>轉(zhuǎn)換
- 解決String字符串轉(zhuǎn)JSONObject順序不對(duì)的問(wèn)題
相關(guān)文章
MyBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例
下面小編就為大家?guī)?lái)一篇MyBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04使用Servlet Filter實(shí)現(xiàn)系統(tǒng)登錄權(quán)限
這篇文章主要為大家詳細(xì)介紹了使用Servlet Filter實(shí)現(xiàn)系統(tǒng)登錄權(quán)限,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Assert.assertEquals的使用方法及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了Assert.assertEquals的使用方法及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05jmeter接口測(cè)試教程及接口測(cè)試流程詳解(全網(wǎng)僅有)
Jmeter是由Apache公司開(kāi)發(fā)的一個(gè)純Java的開(kāi)源項(xiàng)目,即可以用于做接口測(cè)試也可以用于做性能測(cè)試。本文給大家分享jmeter接口測(cè)試教程及接口測(cè)試流程,感興趣的朋友跟隨小編一起看看吧2021-12-12Java8中Optional類(lèi)的使用說(shuō)明
Optional類(lèi)主要解決的問(wèn)題是臭名昭著的空指針異常(NullPointerException),每個(gè)Java程序員都非常了解的異常,這篇文章主要給大家介紹了關(guān)于Java8中Optional類(lèi)使用的相關(guān)資料,需要的朋友可以參考下2021-11-11Java后端實(shí)現(xiàn)生成驗(yàn)證碼圖片的示例代碼
驗(yàn)證碼是一種用于驗(yàn)證用戶身份或確保用戶操作安全的技術(shù)手段,通常以圖形、聲音或文字的形式出現(xiàn),本文主要介紹了如何通過(guò)java實(shí)現(xiàn)生成驗(yàn)證碼圖片,需要的可以參考下2023-12-12Java 中 Form表單數(shù)據(jù)的兩種提交方式
本文給大家分享java中form表單數(shù)據(jù)的兩種提交方式,分別是get從制定的服務(wù)器中獲取數(shù)據(jù),pos方式提交數(shù)據(jù)給指定的服務(wù)器處理,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2016-12-12