问题
I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray.
I have gone through various posts in stack overflow, which suggests using methods like push, put etc which I am unable to identify for JSONArray. Please help.
{
"name": "sample",
"def": [
{
"setId": 1,
"setDef": [
{
"name": "ABC",
"type": "STRING"
},
{
"name": "XYZ",
"type": "STRING"
}
]
},
{
"setId": 2,
"setDef": [
{
"name": "abc",
"type": "STRING"
},
{
"name": "xyz",
"type": "STRING"
}
]
}
]
}
回答1:
With the imports org.json.JSONArray
and org.json.JSONObject
JSONObject object = new JSONObject();
object.put("name", "sample");
JSONArray array = new JSONArray();
JSONObject arrayElementOne = new JSONObject();
arrayElementOne.put("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();
JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.put("name", "ABC");
arrayElementOneArrayElementOne.put("type", "STRING");
JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.put("name", "XYZ");
arrayElementOneArrayElementTwo.put("type", "STRING");
arrayElementOneArray.put(arrayElementOneArrayElementOne);
arrayElementOneArray.put(arrayElementOneArrayElementTwo);
arrayElementOne.put("setDef", arrayElementOneArray);
array.put(arrayElementOne);
object.put("def", array);
I did not include first array's second element for clarity. Hope you got the point though.
EDIT:
The previous answer was assuming you were using org.json.JSONObject
and org.json.JSONArray
.
For net.sf.json.JSONObject
and net.sf.json.JSONArray
:
JSONObject object = new JSONObject();
object.element("name", "sample");
JSONArray array = new JSONArray();
JSONObject arrayElementOne = new JSONObject();
arrayElementOne.element("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();
JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.element("name", "ABC");
arrayElementOneArrayElementOne.element("type", "STRING");
JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.element("name", "XYZ");
arrayElementOneArrayElementTwo.element("type", "STRING");
arrayElementOneArray.add(arrayElementOneArrayElementOne);
arrayElementOneArray.add(arrayElementOneArrayElementTwo);
arrayElementOne.element("setDef", arrayElementOneArray);
object.element("def", array);
Basically it's the same, replacing the method 'put' for 'element' in JSONObject, and 'put' for 'add' in JSONArray.
回答2:
Here is one crude example. You should be able to refine. (You may be interested in this Java "tutorial" http://docs.oracle.com/javaee/7/tutorial/doc/jsonp.htm#GLRBB
(This example uses the JSON reference implementation included in Java EE (and available here: https://java.net/projects/jsonp/downloads/directory/ri)
package com.demo;
import java.io.FileWriter;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
public class JSONExample {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("C:\\Users\\Joseph White\\Downloads\\jsontext.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonGenerator gen = Json.createGenerator(writer);
gen.writeStartObject().write("name", "sample")
.writeStartArray("def")
.writeStartObject().write("setId", 1)
.writeStartArray("setDef")
.writeStartObject().write("name", "ABC").write("type", "STRING")
.writeEnd()
.writeStartObject().write("name", "XYZ").write("type", "STRING")
.writeEnd()
.writeEnd()
.writeEnd()
.writeStartObject().write("setId", 2)
.writeStartArray("setDef")
.writeStartObject().write("name", "abc").write("type", "STRING")
.writeEnd()
.writeStartObject().write("name", "xyz").write("type", "STRING")
.writeEnd()
.writeEnd()
.writeEnd()
.writeEnd()
.writeEnd();
gen.close();
}
}
来源:https://stackoverflow.com/questions/22042638/creating-nested-json-object-for-the-following-structure-in-java-using-jsonobject