le("1.txt")))); System.out.println(obj.getJSONArray("book").getString(1)); //可以读取book2 } } 复杂JSON格式的读取代码:1.txt {"section":{ "title":"BOOK", "signing":[ { "author": { "name":"author-1" }, "book": { "title":"book1", "price":"$11" } }, { "author": { "name":"author-2" }, "book": { "title":"book2", "price":"$22" } } ]} } } package com.xiazdong.json; import java.io.File; import java.io.FileReader; import weibo4j.org.json.JSONObject; import weibo4j.org.json.JSONTokener; publicclass JsonStringerDemo { publicstaticvoid main(String[] args) throws Exception{ JSONObject obj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt")))); System.out.println(obj.getJSONObject("section").getJSONArray("signing").getJSONObject(0)
.getJSONObject("author").getString("name")); <span style="white-space:pre"> </span>//获取author-1 } } 总结:1在Java中JSON格式的String最好用单引号表示;2.利用JSONObject+JSONTokener能够读取JSON格式文件对象;3.利用PrintWriter+JSONStringer可以写入JSON文件;注:由于原本想要试图用JSONWriter写入,但是没有成功;所以只能利用JSONStringer+PrintWriter写入;