Java Gson:使用 JsonStreamParser 解析 JSON 流


#Java Gson#


JSON 流 是指一个数据源中,有多个 JSON 对象。

代码示例:

package org.example;

import com.google.gson.JsonElement;
import com.google.gson.JsonStreamParser;
import org.junit.jupiter.api.Test;

public class TestJsonStreamParser {

    @Test
    public void test() {
        String jsonStr = "[1,2,3,4] {\"name\": \"xiaoming\"} true";
        JsonStreamParser jsonStreamParser = new JsonStreamParser(jsonStr);
        while (jsonStreamParser.hasNext()) {
            JsonElement jsonElement = jsonStreamParser.next();
            System.out.println("解析结果: " + jsonElement);
        }
    }

}

执行结果:

解析结果: [1,2,3,4]
解析结果: {"name":"xiaoming"}
解析结果: true

( 本文完 )