package com.xiazdong.json; import java.io.File; import java.io.FileReader; import weibo4j.org.json.JSONArray; import weibo4j.org.json.JSONObject; import weibo4j.org.json.JSONStringer; import weibo4j.org.json.JSONTokener; public class JsonStringerDemo { public static void main(String[] args) throws Exception{ JSONStringer js = new JSONStringer(); JSONObject obj2 = new JSONObject(); JSONObject obj3 = new JSONObject(); JSONObject obj4 = new JSONObject(); obj4.put("title", "book1").put("price", "$11"); obj3.put("book", obj4); obj3.put("author", new JSONObject().put("name", "author-1")); JSONObject obj5 = new JSONObject(); JSONObject obj6 = new JSONObject(); obj6.put("title", "book2").put("price", "$22"); obj5.put("book", obj6); obj5.put("author", new JSONObject().put("name", "author-2")); JSONArray obj7 = new JSONArray(); obj7.put(obj3).put(obj5); obj2.put("title","BOOK"); obj2.put("signing", obj7); js.object().key("session").value(obj2).endObject(); System.out.println(js.toString()); PrintWriter out = new PrintWriter(new FileOutputStream(1.txt)); out.println(js.toString()); } }