我正在尝试解析JSON对象,但我一直收到错误
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was NAME at line 1 column 3 path $.
JSON对象是
{"hero_id":1,"rankings":[{"account_id":168340162,"score":480.772994325575,"personaname":"period","name":"tmt","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d1/d108bc7c584279b3bfd6147638145b596b5a3649.jpg","last_login":"2017-05-28T18:49:16.421Z","solo_competitive_rank":8883},{"account_id":122049498,"score":457.296281991582,"personaname":"Mike Ross","name":null,"avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/74/745695c8d79c0967ce5cbb63aab90174d0ff427b.jpg","last_login":"2017-08-05T21:55:15.653Z","solo_competitive_rank":7124},{"account_id":178032556,"score":403.130664542372,"personaname":"Never","name":null,"avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/1b/1b07713e510b7c5ca75c0b9b45a6e44182e64e0c.jpg","last_login":"2016-10-26T17:51:51.451Z","solo_competitive_rank":8067},{"account_id":113995822,"score":372.559981235015,"personaname":"iLTW","name":"iLTW","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/9a/9a2a4e758879c0a8851839f0385028c9cef6ac70.jpg","last_login":"2017-01-20T17:11:50.488Z","solo_competitive_rank":8890}]}
简而言之,但json是有效的。我检查了多个网站。
我的课程
public class Rankings {
int hero_id;
Ranks[] rankses = null;
public int getHero_id() {
return hero_id;
}
public Ranks[] getRankses() {
return rankses;
}}
public class Ranks {
@SerializedName("account_id")
int account_id;
@SerializedName("score")
float score;
@SerializedName("personaname")
String personaname;
@SerializedName("name")
String name;
@SerializedName("avatar")
String avatar;
@SerializedName("last_login")
String last_login;
@SerializedName("solo_competitive_rank")
int solo_competitive_rank;
public int getAccount_id() {
return account_id;
}
public float getScore() {
return score;
}
public String getPersonaname() {
return personaname;
}
public String getName() {
return name;
}
public String getAvatar() {
return avatar;
}
public String getLast_login() {
return last_login;
}
public int getSolo_competitive_rank() {
return solo_competitive_rank;
}}
我读取json的代码块是
JsonReader reader = new JsonReader(new InputStreamReader(conn.getInputStream()));
/*JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(reader).getAsJsonObject();
rank = gson.fromJson(obj.get("rankings"), Ranks[].class);*/
reader.beginObject();
rank = gson.fromJson(reader, Rankings.class);
reader.endObject();
reader.close();
当我将reader.beginObject中的代码注释到reader.endObject并取消注释注释部分时,我确实得到了所需的东西。
但我想知道造成错误的原因。我在某个地方犯了错误吗?
答案 0 :(得分:0)
您需要使用注释定义排名类。像这样:
public class Ranking{
@SerializedNme("hero_id")
private int hero_id;
@SerialixedName("ranking")
private Ranks[] ranks;
}
答案 1 :(得分:0)
您需要执行以下操作:
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("rankings")) {
reader.beginArray();
while (reader.hasNext()) {
Ranks yourModelledObject = gson.fromJson(reader, Ranks.class);
//do your thing
}
reader.endArray();
} else
reader.skipValue();
}
reader.endObject();
reader.close();
希望这有帮助,它对我有用!