我正在尝试按以下格式创建JSON
-
{
"approvable" : "true",
"approvedBy" : [user1, user2, user3, user4]
}
我有两个java变量 -
boolean approvable;
List<User> approvedBy;
如果approvedBy
列表包含所有Users
,那么我可以直接使用任何JSON
库制作JSON
吗?假设我想做这样的事情 -
JsonBuilder jb = new JsonBuilder();
jb.add("approvable", true);
jb.add("approvedBy", approvedBy);
最终,我需要做的就是简化JSON的制作。任何人都可以建议任何好的JSON库给我这个支持吗?
提前致谢
答案 0 :(得分:2)
使用的API是flexjson
public static void main(String args[]) throws Exception {
Child child2 = new Child("ankur", 23);
Child child1 = new Child("ankurs", 23);
Child child3 = new Child("ankurss", 21);
List<Child> childList = new ArrayList<Child>();
childList.add(child1);
childList.add(child2);
childList.add(child3);
Parent parent = new Parent();
parent.setApprovable(true);
parent.setChildList(childList);
JSONSerializer serializer = new JSONSerializer();
System.out.println(serializer.exclude("*.class").deepSerialize(parent));
}
<强>输出强>
{
"approvable": true,
"childList": [
{
"age": 23,
"name": "ankurs"
},
{
"age": 23,
"name": "ankur"
},
{
"age": 21,
"name": "ankurss"
}
]
}