利用fastjson解析大文本JSON

157 篇文章 5 订阅
8 篇文章 0 订阅
public static void readBigJson(){
        String json = "{" +
                "\"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\"" +
                "}";
    // 如果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");
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值