如何使用json.org/java库解析JSON文件?

时间:2011-07-26 10:27:09

标签: java json

File f = new File("output.json");

FileReader fr = new FileReader(f);

BufferedReader br = new BufferedReader(fr);

StringBuffer sb = new StringBuffer();

String eachLine = br.readLine();

while (eachLine != null) {

   sb.append(eachLine);

   eachLine = br.readLine();

}

String readFile = sb.toString();

System.out.println("output = "+readFile);

JSONObject output = new JSONObject(readFile);

System.out.println("output jsonobject = "+output.toString());

我的输出不同,看起来很奇怪..

output = {"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"402","height":"604","imageId":"ANd9GcTrNHp9D5SplSwOOfoFtRF8cR5FsQ-A_6TWSk0Dorj6HrEmgUFFGMoUivBZ","tbWidth":"90","tbHeight":"135","unescapedUrl":"http://www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg","url":"http://www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg","visibleUrl":"www.24dhakanews.com","title":"\u003cb\u003eProva\u003c/b\u003e video is for doing business\u0026lt;!--DONTREWRITE--\u0026gt; « Film « Dhaka \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Prova video is for doing business\u0026lt;!--DONTREWRITE--\u0026gt; « Film « Dhaka ...","originalContextUrl":"http://www.24dhakanews.com/film/prova-video-is-for-doing-business.php","content":"date is the \u003cb\u003eProva\u003c/b\u003e scandal.","contentNoFormatting":"date is the Prova scandal.","tbUrl":"http://images.google.com/images?q\u003dtbn:ANd9GcTrNHp9D5SplSwOOfoFtRF8cR5FsQ-A_6TWSk0Dorj6HrEmgUFFGMoUivBZ:www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg"}],"cursor":{"pages":[{"start":"0","label":1},{"start":"1","label":2},{"start":"2","label":3},{"start":"3","label":4},{"start":"4","label":5},{"start":"5","label":6},{"start":"6","label":7},{"start":"7","label":8}],"estimatedResultCount":"19400000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dprova"}}, "responseDetails": null, "responseStatus": 200}

output jsonobject = {"responseData":{"cursor":{"moreResultsUrl":"http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=prova","currentPageIndex":0,"pages":[{"start":"0","label":1},{"start":"1","label":2},{"start":"2","label":3},{"start":"3","label":4},{"start":"4","label":5},{"start":"5","label":6},{"start":"6","label":7},{"start":"7","label":8}],"estimatedResultCount":"19400000"},"results":[{"titleNoFormatting":"Prova video is for doing business&lt;!--DONTREWRITE--&gt; « Film « Dhaka ...","tbUrl":"http://images.google.com/images?q=tbn:ANd9GcTrNHp9D5SplSwOOfoFtRF8cR5FsQ-A_6TWSk0Dorj6HrEmgUFFGMoUivBZ:www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg","originalContextUrl":"http://www.24dhakanews.com/film/prova-video-is-for-doing-business.php","width":"402","unescapedUrl":"http://www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg","url":"http://www.24dhakanews.com/wp-content/uploads/2011/04/prova05.jpg","visibleUrl":"www.24dhakanews.com","GsearchResultClass":"GimageSearch","tbWidth":"90","content":"date is the <b>Prova<\/b> scandal.","title":"<b>Prova<\/b> video is for doing business&lt;!--DONTREWRITE--&gt; « Film « Dhaka <b>...<\/b>","height":"604","imageId":"ANd9GcTrNHp9D5SplSwOOfoFtRF8cR5FsQ-A_6TWSk0Dorj6HrEmgUFFGMoUivBZ","contentNoFormatting":"date is the Prova scandal.","tbHeight":"135"}]},"responseDetails":null,"responseStatus":200}

1 个答案:

答案 0 :(得分:1)

第一个打印输出已转义字符,第二个打印输出没有。

他们都是一样的。在第二个打印输出中,您将返回的JSON字符串文字解析为JSON对象,然后将其转换为String。

您的第一次打印输出:

"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dprova"

你的第二张照片:

"moreResultsUrl":"http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=prova"

您可以试试parser