public static void readBigJson(){ String json = "{" + "\"array\": [1,2,3]," + "\"arraylist\": [" + "{\"a\": \"b\"," + "\"c\": \"d\"," + "\"e\": \"f\"}," + "{\"a\": \"b\"," + "\"e\": \"f\"} " + "]," + "\"object\": {" + "\"a\": \"b\"," + "\"c\": \"d\"," + "\"e\": \"f\"}," + "\"string\": \"Hello World\"" + "}"; // 如果json数据以形式保存在文件中,用FileReader进行流读取,path为json数据文件路径。 // JSONReader reader = new JSONReader(new FileReader(path)); // 为了直观,方便运行,就用StringReader做示例! JSONReader reader = new JSONReader(new StringReader(json)); reader.startObject(); System.out.print("start read json with fastjson"); while (reader.hasNext()) { String key = reader.readString(); System.out.println("key " + key); if (key.equals("array")) { reader.startArray(); System.out.println("start " + key); while (reader.hasNext()) { String item = reader.readString(); System.out.println(item); } reader.endarray(); System.out.println("end " + key); } else if (key.equals("arraylist")) { reader.startArray(); System.out.println("start " + key); while (reader.hasNext()) { reader.startObject(); System.out.println("start arraylist item"); while (reader.hasNext()) { String arrayListItemKey = reader.readString(); String arrayListItemValue = reader.readobject().toString(); System.out.print("key " + arrayListItemKey); System.out.println(":value " + arrayListItemValue); } reader.endobject(); System.out.println("end arraylist item"); } reader.endarray(); System.out.println("end " + key); } else if (key.equals("object")) { reader.startObject(); System.out.println("start object item"); while (reader.hasNext()) { String objectKey = reader.readString(); String objectValue = reader.readobject().toString(); System.out.print("key " + objectKey); System.out.println(":value " + objectValue); } reader.endobject(); System.out.println("end object item"); } else if (key.equals("string")) { System.out.println("start string"); String value = reader.readobject().toString(); System.out.println("value " + value); System.out.println("end string"); } } reader.endobject(); System.out.println("start fastjson"); }
原文地址:https://www.jb51.cc/json/288759.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。