加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

利用fastjson解析大文本JSON

发布时间:2020-12-16 18:52:07 所属栏目:百科 来源:网络整理
导读:public static void readBigJson (){ String json = "{" + " " array " : [1,2,3]," + " " arraylist " : [" + "{ " a " : " b " ," + " " c " : " d " ," + " " e " : " f " }," + "{ " a " : " b " ," + " " e " : " f " } " + "]
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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读