VerTiGo_Etrex
VerTiGo_Etrex

Reputation: 149

GSON parse array of array into Java array of object

I have an JSON array of arrays where none of the elements are named (it's just a pure array)

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

In Java, I'd like to parse this JSON into an array of connectionobjs, where the connectionobj class looks like this:

public static class connectionOptions {
    String URL, RESID;
}

I looked through the GSON documentation, but couldn't seem to find anything pertinent to parsing a JSON array into anything other than another Java Array. I want to parse the JSON array into a Java Object, not an array.

Is there a way to do this using Google's GSON?

Upvotes: 0

Views: 564

Answers (2)

Jeff Liu
Jeff Liu

Reputation: 366

You should give a customized Deserializer.

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);
    }

}

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

I don't recommend this at all. You should try to have appropriate JSON that maps correctly to Pojos.

If you can't change your JSON format, you'll need to register a custom TypeAdapter that can do the conversion. Something like

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;
    }
}

Then just register it

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

Gson gson = gsonBuilder.create();

and use it.

Deserialize your JSON as an ConnectionOptions[] or List<ConnectionOptions>.


I've change your class name to ConnectionOptions to follow Java naming conventions.

Upvotes: 1

Related Questions