1

So I have been looking around and I am not sure exactly how to handle this. I am parsing JSON that looks like this

{
    "foo1" : { /* data I want */ },
    "foo2" : { /* data I want */ },
    "foo3" : { /* data I want */ },
    ...
}

Since I am trying to go through a large file I am using a stream. What I have right now is

gson = new GsonBuilder().create();

JsonReader jReader = new JsonReader(new InputStreamReader(getResources()
                                            .openRawResource(R.raw.all_sets_x), "UTF-8"));

while (jReader.hasNext() && jReader.peek() != JsonToken.END_DOCUMENT) {
    Set set = gson.fromJson(jReader, Set.class);
    sets.add(set);
}

My issue is I want to skip the foo, and just read the { /* data I want */ }. I don't know offhand what all the different foos I may get. When I try to skip it skips the outer braces, but when I beginObject it errors on a string inside the braces with the data I want. I am sure I am likely missing something simple, but for the life of me I am not sure. I looked at trying to use the beginObject to make a json object and parse that using gson, but I was unsucessful there as well. Thanks for the help!

Community
  • 1
  • 1
  • you have a list/arry of foos, why not deserialize this and go thougth in a for loop??? – ΦXocę 웃 Пepeúpa ツ Feb 17 '16 at 16:48
  • the file is too large, I have to read it in as a stream, otherwise I get an out of memory exception. As well I do not know the names of all the foos, so I don't have an object I can deserialize it to. I am still learning the json stuff so I could just be doing something dumb. – thechucklingatom Feb 17 '16 at 16:50
  • It sounds like you want a SAX parser rather than a DOM parser - see this [related question](http://stackoverflow.com/questions/444380/is-there-a-streaming-api-for-json). – Andrew Williamson Feb 17 '16 at 17:29
  • I don't understand right now what that changes for me, its just using a different parser, and the parser (gson) I am using is listed in that question. – thechucklingatom Feb 17 '16 at 17:46

1 Answers1

0

Ok, so I figured it out. I spaced that Name, which was erroring while it is a field in my object it is also JSON for the title of the object in this case (foo1). I found that I can get into the first object by going

jsonReader.beginObject();

jsonReader.nextName();

while (jsonReader.hasNext() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
    Set set = gson.fromJson(jsonReader, Set.class);
    sets.add(set);
    jsonToken = jsonReader.peek();
    if (jsonToken == JsonToken.NAME) {
        jsonReader.nextName();
    } else if (jsonToken != JsonToken.END_DOCUMENT) {
        jsonReader.endObject();
        jsonReader.skipValue();
        jsonToken = jsonReader.peek();
        if (jsonToken != JsonToken.END_DOCUMENT) {
            jsonReader.nextName();
        }
    }
}

This allows me to get the { /* data I want */ } that is after the first foo, and all the foos after it, I have to check if it is a name or not because I believe there is an extra end object somewhere in the json object.