利用JSONObject.toJSONString()包含或排除指定的屬性
JSONObject.toJSONString包含或排除指定的屬性
將一個實體對象轉(zhuǎn)換成Json字符串 JSON.toJSONString()
FastJson提供的SerializeFilter類可以指定轉(zhuǎn)換時要包含的屬性,或者指定轉(zhuǎn)換時要排除的屬性。
JSONObject.toJSONString()默認(rèn)忽略值為null的屬性.
使用JSONObject提供的以下方法將實體對象轉(zhuǎn)換成Json字符串:(JSONObject 提供的toJSONString 源碼 自己還沒看)
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將實體對象轉(zhuǎn)換成Json字符串測試類 */ 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("中國"); user.setCity("武漢"); String jsonUser = null; /** * 指定排除屬性過濾器和包含屬性過濾器 * 指定排除屬性過濾器:轉(zhuǎn)換成JSON字符串時,排除哪些屬性 * 指定包含屬性過濾器:轉(zhuǎn)換成JSON字符串時,包含哪些屬性 */ 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這兩個屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat); System.out.println("情況三:\n" + jsonUser); /** * 情況四:包含值為null的屬性,但是排除country和city這兩個屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況四:\n" + jsonUser); /** * 情況五:默認(rèn)忽略值為null的屬性,但是包含id、username和mobile這三個屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat); System.out.println("情況五:\n" + jsonUser); /** * 情況六:包含值為null的屬性,但是包含id、username和mobile這三個屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況六:\n" + jsonUser); } /** * 用戶實體類 */ 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方法 }
運行結(jié)果:
結(jié)果說明:
- 情況一和情況二說明了public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature… features)這個方法將實體對象轉(zhuǎn)換成JSON字符串時,默認(rèn)是忽略掉值為null的屬性,并且說明了如何使得轉(zhuǎn)換后的JSON字符串包含值為null的屬性。
- 情況三和情況四說明了如何使用SerializeFilter來排除指定屬性,使得轉(zhuǎn)換后的JSON字符串中不包含這些屬性。
- 情況五和情況六說明了如何使用SerializeFilter來包含指定屬性,使得轉(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類型的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é)果
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- String轉(zhuǎn)JSONObject的兩種方式
- Java中如何將String轉(zhuǎn)JSONObject
- 關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問題
- 使用JSONObject.toJSONString 過濾掉值為空的key
- JSONObject?toJSONString錯誤的解決
- 解決JSONObject.toJSONString()輸出null的問題
- Java使用fastjson對String、JSONObject、JSONArray相互轉(zhuǎn)換
- 詳解Java中String JSONObject JSONArray List<實體類>轉(zhuǎn)換
- 解決String字符串轉(zhuǎn)JSONObject順序不對的問題
相關(guān)文章
使用Servlet Filter實現(xiàn)系統(tǒng)登錄權(quán)限
這篇文章主要為大家詳細(xì)介紹了使用Servlet Filter實現(xiàn)系統(tǒng)登錄權(quán)限,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10Assert.assertEquals的使用方法及注意事項說明
這篇文章主要介紹了Assert.assertEquals的使用方法及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05jmeter接口測試教程及接口測試流程詳解(全網(wǎng)僅有)
Jmeter是由Apache公司開發(fā)的一個純Java的開源項目,即可以用于做接口測試也可以用于做性能測試。本文給大家分享jmeter接口測試教程及接口測試流程,感興趣的朋友跟隨小編一起看看吧2021-12-12Java 中 Form表單數(shù)據(jù)的兩種提交方式
本文給大家分享java中form表單數(shù)據(jù)的兩種提交方式,分別是get從制定的服務(wù)器中獲取數(shù)據(jù),pos方式提交數(shù)據(jù)給指定的服務(wù)器處理,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2016-12-12