Python 官方文档:入门教程 => 点击学习
目录JSONObject.tojsONString包含或排除指定的属性演示程序JSONObject toJSONString 遇到的坑引入pom文件使用JSONObject 输出 i
将一个实体对象转换成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();
}
}
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
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;
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);
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFORMat);
System.out.println("情况一:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("情况二:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat);
System.out.println("情况三:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("情况四:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat);
System.out.println("情况五:\n" + jsonUser);
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方法
}
运行结果:
结果说明:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
<scope>test</scope>
</dependency>
是非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));
}
}
输出结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 利用JSONObject.toJSONString()包含或排除指定的属性
本文链接: https://lsjlt.com/news/141133.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0