【问题标题】:GSON parse array of array into Java array of objectGSON 将数组数组解析为 Java 对象数组
【发布时间】:2025-12-02 07:45:02
【问题描述】:

我有一个 JSON 数组,其中没有任何元素被命名(它只是一个纯数组)

[
    ["http://test.com/","rj76-22dk"],
    ["http://othertest.com/","v287-28n3"]
]

在Java中,我想把这个JSON解析成一个connectionobjs数组,其中connectionobj类看起来像这样:

public static class connectionOptions {
    String URL, RESID;
}

我查看了 GSON 文档,但似乎找不到与将 JSON 数组解析为其他 Java 数组以外的任何内容相关的任何内容。我想将 JSON 数组解析为 Java 对象,而不是数组。

有没有办法使用 Google 的 GSON 来做到这一点?

【问题讨论】:

  • 不是直接的。你必须自己写TypeAdapter

标签: java javascript arrays json gson


【解决方案1】:

我完全不推荐这个。您应该尝试使用正确映射到 Pojos 的适当 JSON。

如果您无法更改 JSON 格式,则需要注册一个可以进行转换的自定义 TypeAdapter。类似的东西

class ConnectionOptionsTypeAdapter extends TypeAdapter<ConnectionOptions> {

    @Override
    public void write(JsonWriter out, ConnectionOptions value)
            throws IOException {
        // implement if you need it
    }

    @Override
    public ConnectionOptions read(JsonReader in) throws IOException {
        final ConnectionOptions connectionOptions = new ConnectionOptions();

        in.beginArray();
        connectionOptions.URL = in.nextString();
        connectionOptions.RESID = in.nextString();
        in.endArray();

        return connectionOptions;
    }
}

那就注册吧

GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(
            ConnectionOptions.class, new ConnectionOptionsTypeAdapter());

Gson gson = gsonBuilder.create();

并使用它。

将您的 JSON 反序列化为 ConnectionOptions[]List&lt;ConnectionOptions&gt;


我已将您的类名更改为 ConnectionOptions 以遵循 Java 命名约定。

【讨论】:

    【解决方案2】:

    您应该提供一个自定义的反序列化器。

    import com.google.gson.*;
    import com.google.gson.reflect.TypeToken;
    
    import java.lang.reflect.Type;
    import java.util.Collection;
    
    public class TestGson {
    
        public static class ConnectionOptions {
            String URL, RESID;
    
            @Override
            public String toString() {
                return "ConnectionOptions{URL='" + URL + "', RESID='" + RESID + "'}";
            }
        }
    
        private static class ConnOptsDeserializer implements JsonDeserializer<ConnectionOptions> {
            @Override
            public ConnectionOptions deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                ConnectionOptions connOpts = new TestGson.ConnectionOptions();
                JsonArray array = json.getAsJsonArray();
                connOpts.URL = array.get(0).getAsString();
                connOpts.RESID = array.get(1).getAsString();
                return connOpts;
            }
        }
    
        public static void main(String[] args) {
            String json = "[[\"http://test.com/\",\"rj76-22dk\"],\n" +
                    "    [\"http://othertest.com/\",\"v287-28n3\"]]";
    
            GsonBuilder gsonb = new GsonBuilder();
            gsonb.registerTypeAdapter(ConnectionOptions.class, new ConnOptsDeserializer());
            Gson gson = gsonb.create();
    
            Type collectionType = new TypeToken<Collection<ConnectionOptions>>(){}.getType();
            Collection<ConnectionOptions> connList = gson.fromJson(json, collectionType);
            System.out.println("connList = " + connList);
        }
    
    }
    

    【讨论】: