使用jettison,您可以执行以下操作:
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class JSONTest {
public static void main(final String[] args) throws JSONException {
final JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "Maximum");
final List<JSONObject> children = new ArrayList<JSONObject>();
final JSONObject child1 = new JSONObject();
child1.put("name", "where");
child1.put("size", 299);
children.add(child1);
final JSONObject child2 = new JSONObject();
child2.put("name", "xor");
child2.put("size", 354);
children.add(child2);
final JSONObject child3 = new JSONObject();
child3.put("name", "_");
child3.put("size", 264);
children.add(child3);
jsonObject.put("children", children);
System.out.println(jsonObject.toString());
}
}