开发者

How to parse JSON File with json.org/java libraries?

开发者 https://www.devze.com 2023-03-23 03:00 出处:网络
File f = new File(\"output.json\"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr);
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());

And my output are different and they appear strange..

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\u0开发者_如何转开发026q\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}


The first printout has escaped characters, the second one doesn't.

They are both the same. In the second printout, you are parsing the returned JSON string literal into a JSON object, then turning it into a String.

Your first printout:

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

Your second prinout:

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

You can try this parser.

0

精彩评论

暂无评论...
验证码 换一张
取 消