记录心得-FastJson分层解析demo示例

记录一下,平时用到,可速查!
关键:
//        startArray(); 开始解析数组
//        endArray(); 结束解析数组
//        startObject(); 开始解析键值对
//        endObject(); 结束解析键值对
需要用到的jar包:http://repo1.maven.org/maven2/com/alibaba/fastjson/1.2.6/fastjson-1.2.6.jar




demoJson:{
  "array": [
    1,
    2,
    3
  ],
  "arraylist": [
    {
      "a": "b",
      "c": "d",
      "e": "f"
    },
    {
      "a": "b",
      "c": "d",
      "e": "f"
    },
    {
      "a": "b",
      "c": "d",
      "e": "f"
    }
  ],
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
}


/**
* Copyright (C), 2015-2019, XXX有限公司 * FileName: FastJsonTest * Author: zhang.tong2 * Date: 2019/2/26 9:03 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.tong.appoint.web; import com.alibaba.fastjson.JSONReader; import java.io.StringReader; /** * 〈一句话功能简述〉<br> * 〈〉 * * @author zhang.tong2 * @create 2019/2/26 * @since 1.0.0 */ public class FastJsonTest { /** * FastJson逐行解析json * @author drlyee * @date 2015-02-10 */ public static void main(String[] args){ ReadWithFastJson(); } public static void ReadWithFastJson() { String jsonString = "{"array":[1,2,3],"arraylist":[{"a":"b","c":"d","e":"f"},{"a":"b","c":"d","e":"f"},{"a":"b","c":"d","e":"f"}],"object":{"a":"b","c":"d","e":"f"},"string":"HelloWorld"}"; // 如果json数据以形式保存在文件中,用FileReader进行流读取!! // path为json数据文件路径!! // JSONReader reader = new JSONReader(new FileReader(path)); // 为了直观,方便运行,就用StringReader做示例! // startArray(); 开始解析数组 // endArray(); 结束解析数组 // startObject(); 开始解析键值对 // endObject(); 结束解析键值对 JSONReader reader = new JSONReader(new StringReader(jsonString)); reader.startObject(); System.out.print("start fastjson"); while (reader.hasNext()) { String key = reader.readString(); System.out.print("key " + key); if (key.equals("array")) { reader.startArray(); System.out.print("start " + key); while (reader.hasNext()) { String item = reader.readString(); System.out.print(item); } reader.endArray(); System.out.print("end " + key); } else if (key.equals("arraylist")) { reader.startArray(); System.out.print("start " + key); while (reader.hasNext()) { reader.startObject(); System.out.print("start arraylist item"); while (reader.hasNext()) { String arrayListItemKey = reader.readString(); String arrayListItemValue = reader.readObject().toString(); System.out.print("key " + arrayListItemKey); System.out.print("value " + arrayListItemValue); } reader.endObject(); System.out.print("end arraylist item"); } reader.endArray(); System.out.print("end " + key); } else if (key.equals("object")) { reader.startObject(); System.out.print("start object item"); while (reader.hasNext()) { String objectKey = reader.readString(); String objectValue = reader.readObject().toString(); System.out.print("key " + objectKey); System.out.print("value " + objectValue); } reader.endObject(); System.out.print("end object item"); } else if (key.equals("string")) { System.out.print("start string"); String value = reader.readObject().toString(); System.out.print("value " + value); System.out.print("end string"); } } reader.endObject(); System.out.print("start fastjson"); } }



原文地址:https://www.cnblogs.com/tong0637/p/tong0637.html