我找到了很多例子来解析Java对象到JSON。
谢谢。但是我有一个问题,我想要以下格式的json
{
"parent":
{
"sub-parent-1":
{
"child-1": 1,
"child-2": 2
},
"sub-parent-2":
{
"child-2": 3
}
}
}
有可能使用Java。
请回答。谢谢提前…
最佳答案
在Java中创建当前JSON字符串:
JSONObject parent = new JSONObject();
JSONObject subparentone = new JSONObject();
JSONObject subparenttwo = new JSONObject();
subparentone.put("child-1", "1");
subparentone.put("child-2", "2");
subparenttwo.put("child-2", "3");
parent.put("sub-parent-1", subparentone);
parent.put("sub-parent-2", subparenttwo);
JSONObject finalparent = new JSONObject();
finalparent.put("parent", parent);
最后将jsonobject输出为:
{
"parent": {
"sub-parent-1": {
"child-1": 1,
"child-2": 2
},
"sub-parent-2": {
"child-2": 3
}
}
}
关于java - 如何在从Java Object解析的json数组中添加Json header ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13995024/