Java Gson:JsonElement 及其子类的使用


#Java Gson#


JsonElement 是一个抽象类,子类有 JsonArray、JsonObject、JsonNull、JsonPrimitive 等。

如何使用

示例: 获取某个 key 的值

package org.example;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import org.junit.jupiter.api.Test;

public class TestJsonElement {

    @Test
    public void test() {
        String jsonData = "{\"age\":18, \"height\":180}";
        JsonElement jsonElement = JsonParser.parseString(jsonData);
        System.out.println(jsonElement instanceof JsonObject);
        // 以上代码输出: true
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        JsonElement ageValue = jsonObject.get("age");
        System.out.println(ageValue);
        // 以上代码输出: 18
        System.out.println(ageValue instanceof JsonPrimitive);
        // 以上代码输出: true
        int age = ageValue.getAsInt();
        System.out.println(age);
        // 以上代码输出: 18
    }
}

执行结果:

true
18
true
18

示例: 移除某个 key

代码:

package org.example;

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

public class TestJsonElement {

    @Test
    public void test() {
        String jsonData = "{\"age\":18, \"height\":180}";
        JsonElement jsonElement = JsonParser.parseString(jsonData);
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        System.out.println("移除 age 前: " + jsonElement);

        System.out.println("-- 执行移除操作 --");
        jsonObject.remove("age");

        System.out.println("移除 age 后: " + jsonElement);
    }
}

执行结果:

移除 age 前: {"age":18,"height":180}
-- 执行移除操作 --
移除 age 后: {"height":180}

示例: 添加键值对

add、addProperty 方法可以添加键值对。

代码:

package org.example;

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

public class TestJsonElement {

    @Test
    public void test() {
        String jsonData = "{\"age\":18, \"height\":180}";
        JsonElement jsonElement = JsonParser.parseString(jsonData);
        JsonObject jsonObject = jsonElement.getAsJsonObject();

        System.out.println("添加前: " + jsonElement);

        System.out.println("-- 执行添加操作 --");
        jsonObject.addProperty("name", "李白");

        System.out.println("添加后: " + jsonElement);
    }
}

执行结果:

添加前: {"age":18,"height":180}
-- 执行添加操作 --
添加后: {"age":18,"height":180,"name":"李白"}

示例: 获取数组中的每个元素

package org.example;

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

public class TestJsonElement {

    @Test
    public void test() {
        String jsonData = "[101, \"102\"]";
        JsonElement jsonElement = JsonParser.parseString(jsonData);
        JsonArray jsonArray = jsonElement.getAsJsonArray();

        JsonElement item0 = jsonArray.get(0);
        int itemValue0 = item0.getAsInt();
        System.out.println(itemValue0);
        // 以上代码输出: 101

        JsonElement item1 = jsonArray.get(1);
        String itemValue1 = item1.getAsString();
        System.out.println(itemValue1);
        // 以上代码输出: 102
    }
    
}

( 本文完 )