我正在使用jsonreader解析一些json数据。这是一大串结构相似的元素:

[
   {
      "type":"duel",
      "discipline":"leagueoflegends",
      "tournament_id":"57ee13fe140ba0cd2a8b4593",
      "opponents":[
         {
            "participant":null,
            "forfeit":false,
            "number":1,
            "result":1,
            "score":3
         },
         {
            "participant":null,
            "forfeit":false,
            "number":2,
            "result":3,
            "score":2
         }
      ],
      "id":"58078b2770cb49c45b8b45bf",
      "status":"completed",
      "number":1,
      "stage_number":1,
      "group_number":1,
      "round_number":1,
      "date":"2016-10-01T05:00:00+0300",
      "timezone":"Europe/Helsinki",
      "match_format":null
   },
   ...
]

这个数组中有数百个类似的元素。我只需要每个元素的一些数据,我编写了类似于this的代码:
public List<Match> readJsonStream(InputStream in) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    try {
        return readMatchesArray(reader);
    } finally {
        reader.close();
    }
}

public List<Match> readMatchesArray(JsonReader reader) throws IOException {
    List<Match> matches = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        matches.add(readMatch(reader));
    }
    reader.endArray();
    return matches;
}

public Match readMatch(JsonReader reader) throws IOException {
    String status = null, date = null, time = null;
    Match.Bracket bracket = null;
    int id = -1, round = -1;
    Match.Team team1 = null, team2 = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
            case "number":
                id = reader.nextInt();
                break;
            case "status":
                status = reader.nextString().toUpperCase();
                break;
            case "group_number":
                if (reader.nextInt() == 1) {
                    bracket = WINNERS;
                } else if (reader.nextInt() == 2) {
                    bracket = LOSERS;
                }
                break;
            case "round_number":
                round = reader.nextInt();
                break;
            case "date":
                try {
                    String tempDateRaw = reader.nextString();
                    String[] tempDate = tempDateRaw.split("T");
                    String[] tempTime = tempDate[1].split(":");

                    date = tempDate[0];
                    time = tempTime[0] + ":" + tempTime[1];
                } catch (IllegalStateException e) {
                    date = null;
                    time = null;
                }
                break;
            case "opponents":
                int counter = 0;
                reader.beginArray();
                while (reader.hasNext()) {
                    if (counter == 0) {
                        team1 = readTeam(reader);
                    } else {
                        team2 = readTeam(reader);
                    }
                    counter++;
                }
                reader.endArray();
                break;
            default:
                reader.skipValue();
                break;
        }
    }
    reader.endObject();
    return new Match(team1, team2, id, round, bracket, status, date, time);
}


public Match.Team readTeam(JsonReader reader) throws IOException {
    String teamName = null;
    int score = -1;
    boolean winner = false;

    reader.beginObject();
    while (reader.hasNext()) {
            String name = reader.nextName(); //this is where the error occurs
            switch (name) {
                case "participant":
                    if(reader.peek() != JsonToken.NULL) {
                        teamName = reader.nextString();
                    }
                    else {
                        teamName = null;
                    }
                    break;
                case "score":
                    if(reader.peek() != JsonToken.NULL) {
                        score = reader.nextInt();
                    }
                    else {
                        score = -1;
                    }
                    break;
                case "result":
                    if(reader.peek() != JsonToken.NULL) {
                        winner = reader.nextInt() == 1;
                    }
                    else {
                        winner = false;
                    }
                    break;
                default:
                    reader.skipValue();
                    break;
            }
    }
    reader.endObject();

    return new Match.Team(teamName, score, winner);
}

但是,当我试图解析opponents数组时,在readTeam()中出现了一个错误,它需要一个名称,但为空,我很困惑为什么会发生这种情况。
一开始我不知道peek()是什么,所以在readmatch()方法中,我使用了try/catch,我必须改变它,但它不应该与问题相关。我找不到关于这个特殊错误的任何信息,有很多关于其他类似错误的主题(预期名称是string/int/whatever),但是这里我找不到它不起作用的原因。你知道怎么修吗?

最佳答案

你也应该使用null代币,调用nextNull()skipValue(),否则Reader将不会前进,并将坚持使用这个null。例如:

case "result":
    if(reader.peek() != JsonToken.NULL) {
        winner = reader.nextInt() == 1;
    } else {
        winner = false;
        reader.nextNull(); // note on this
    }
    break;

https://developer.android.com/reference/android/util/JsonReader
可以使用nextnull()或skipvalue()使用空文本。

10-06 06:51