阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

JSONObject.parseObject 、JSONObject.toJSONString

39次阅读
没有评论

共计 2168 个字符,预计需要花费 6 分钟才能阅读完成。

导读 在 Java 开发中,通常需要将一个实体对象转换成 Json 字符串,使用 FastJson 来实现这种转换十分方便,只要使用 FastJson 中 JSONObject 静态类提供的 toJSONString() 静态方法即可,但是如果不了解这个方法,很有可能就会使得转换后的 Json 不合自己的要求。
JSONObject.toJSONString

使用 JSONObject 把实体对象转换成 Json 字符串时,如果实体对象中有些属性的值为 null,则默认转换后的 Json 字符串中是不包含这些值为 null 的属性。

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);
    }
JSONObject.parseObject

result 格式:

{
"success":"true",
"data":{"shop_uid":"123"}
JSONObject shop_user =JSON.parseObject(result);

JSON.parseObject(shop_user.getString("data")).getString("shop_uid")

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2168字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中