AquaMorph
AquaMorph

Reputation: 507

GSON nameless array

I have some JSON data that looks like this:

[["Rank", "Team", "Qual Avg", "Auto", "Container", "Coopertition", "Litter", "Tote", "Played"], 
["1", "1225", "60.8", "0", "224", "80", "70", "240", "10"], 
["2", "3506", "55.2", "0", "132", "40", "268", "118", "10"], 
["3", "5511", "53.3", "4", "124", "160", "141", "134", "10"], 
["4", "3336", "51.7", "0", "80", "160", "177", "100", "10"], 
["5", "4073", "49.1", "0", "100", "80", "167", "156", "10"]]

I've been using the GSON library to parse other JSON data with no problem but since these arrays no names. I'm not sure how to make the model for this since there are no names on anything. My fetching script works like this. I'm sure there is something simple I am missing but couldn't find anything in the documentation that would help.

public class EventParsers {
    public String TAG = "EventParsers";
    public volatile boolean parsingComplete = true;
    private Events[] events;
    private ArrayList<Events> eventArray = new ArrayList<>();

    public void fetchJSON(final String number) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gson gson = new Gson();
                    BlueAlliance blueAlliance = new BlueAlliance();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(blueAlliance
                            .connect(Constants.getEventURL(number))));
                    events = gson.fromJson(reader, Events[].class);
                    eventArray = new ArrayList<>(Arrays.asList(events));
                    blueAlliance.close();
                    Log.i(TAG, "URL: " + Constants.getEventURL(number));
                    parsingComplete = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    public ArrayList<Events> getEvents() {
        return eventArray;
    }
}

Upvotes: 1

Views: 319

Answers (1)

Stelium
Stelium

Reputation: 1367

Actually you have an array of arrays of Strings, so all you need is

events = gson.fromJson(reader, String[][].class);

Upvotes: 2

Related Questions