Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm parsing this JSON stream

{
    "status":"ok",
    "count":1,
    "data":{
        "1000194290":[
            {
                "statistics":{
                    "wins":472,
                    "all":{
                        "spotted":0,
                        "hits":0,
                        "battle_avg_xp":0,
                        "draws":0,
                        "wins":472,
                        "losses":0,
                        "capture_points":0,
                        "battles":894,
                        "damage_dealt":0,
                        "hits_percents":0,
                        "damage_received":0,
                        "shots":0,
                        "xp":0,
                        "frags":0,
                        "survived_battles":0,
                        "dropped_capture_points":0
                    },
                    "battles":894
                },
                "mark_of_mastery":4,
                "tank_id":3649
            },
            ...
        ]
    }
}

I attempt to start an array at 1000194290 but get this error

Exception in thread "main" java.lang.IllegalStateException: Expected BEGIN_ARRAY but was NAME at line 1 column 35

My parsing class looks like this

public List<TankStats> readJsonStream(InputStream in) throws IOException {
    try (JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"))) {
        return readTankIdStep(reader);
    }
}

public List<TankStats> readTankIdStep(JsonReader reader) throws IOException {
    List<TankStats> users = new ArrayList<>();
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("data")) {
            reader.beginObject();
            while (reader.hasNext()) {
                reader.beginArray();
                while (reader.hasNext()) {
                    users.add(readTankId(reader));
                 }
                 reader.endArray();
            }
        }   
        else {
            reader.skipValue();
        }   
    }
    reader.endObject();
    return users;
}

I'm not really sure how to get around this error.

share|improve this question
    
Where is your error? Can you post the stack trace and tell us which line is the error? –  Joffrey Apr 28 at 14:09
add comment

1 Answer

You check for the JSON

"data":{

with

if (name.equals("data")) {

Then consume the object in that name value pair with

reader.beginObject();

But then you do this

while (reader.hasNext()) {
    reader.beginArray();

while the token in the reader is

"1000194290":[
        {

You have to first consume the name before you consume the array.

share|improve this answer
    
to be more specific, reader.nextName(); will return "1000194290" at this point. –  njzk2 Apr 28 at 14:15
    
adding reader.nextName(); beforereader.beginArray(); fixed it thanks –  user3581606 Apr 28 at 19:59
    
@user3581606 You're welcome. If the answer fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved, and it gives the person that helps you credit for the assist. –  Sotirios Delimanolis Apr 28 at 20:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.