JsonReader - 读取数组抛出预期的名称但是为NULL

时间:2016-11-06 16:54:37

标签: java android json parsing jsonreader

我使用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);
}

然而,当我试图解析对手阵列时,我在readTeam中遇到错误()预期名称但是为空,我很困惑为什么会发生这种情况

起初我并不知道peek()所以在readMatch()方法中我使用了try / catch,我必须改变它,但它不应该与问题相关。我无法找到关于这个特定错误的任何内容,关于其他类似错误的主题很多(预期名称是字符串/ int /等等),但在这里我找不到原因它不工作。你有任何想法如何解决它?

2 个答案:

答案 0 :(得分:1)

好吧,我发现了一个解决方案,就像我想的那样,它比我想象的要简单。所以基本上当我检查空值时,如下所示:

 if(reader.peek() != JsonToken.NULL) {
   teamName = reader.nextString();
 }
 else {
   teamName = null;
 }

所谓的&#34;读者光标&#34;不会移动,所以它基本上都停留在teamName的值上(在这种情况下)。所以最简单的解决方案就是将reader.skipValue();放在else块中。我还在我的代码中找到了另一个错误,在case "group_number"中,我实际上移动了前面提到的&#34;游标&#34;两次,但解决方案很明显。希望它会对某人有所帮助。

答案 1 :(得分:1)

您也应该使用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()使用空文字。