JSONObject.toJSONString()包含或排除指定的屬性


將一個實體對象轉換成Json字符串 JSON.toJSONString()

FastJson提供的SerializeFilter類可以指定轉換時要包含的屬性,或者指定轉換時要排除的屬性。

 JSONObject.toJSONString()默認忽略值為null的屬性.

 使用JSONObject提供的以下方法將實體對象轉換成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();
        }
    }
View Code

演示程序:

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
/**
 * 使用FastJson將實體對象轉換成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;

        /**
         * 指定排除屬性過濾器和包含屬性過濾器
         * 指定排除屬性過濾器:轉換成JSON字符串時,排除哪些屬性
         * 指定包含屬性過濾器:轉換成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);


        /**
         * 情況一:默認忽略值為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);

        /**
         * 情況三:默認忽略值為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);

        /**
         * 情況五:默認忽略值為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;
    //此處省略了相應屬性的set、get方法
}

運行結果:

 

 結果說明

 

情況一和情況二說明了public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature… features)這個方法將實體對象轉換成JSON字符串時,默認是忽略掉值為null的屬性,並且說明了如何使得轉換后的JSON字符串包含值為null的屬性。
情況三和情況四說明了如何使用SerializeFilter來排除指定屬性,使得轉換后的JSON字符串中不包含這些屬性。
情況五和情況六說明了如何使用SerializeFilter來包含指定屬性,使得轉換后的JSON字符串中只包含這些屬性。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM