zoukankan      html  css  js  c++  java
  • 记录心得-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"); } }


    
    
  • 相关阅读:
    UIScrollView设置滑动的距离
    iOS动画效果和实现
    cmd 编码 utf8
    详细的ifcfg-eth0配置详解
    Centos下 Nginx安装与配置
    linux安装目录
    utf8 和 UTF-8 的区别
    后台添加搜索功能讲解
    移动端 禁横向滚动
    PC端 $_SERVER 说明
  • 原文地址:https://www.cnblogs.com/tong0637/p/tong0637.html
Copyright © 2011-2022 走看看