简体   繁体   中英

GSON Expected Begin_object but was Name JSON

I'm trying to parse the JSON object but I keep getting the error

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 
           Expected BEGIN_OBJECT but was NAME at line 1 column 3 path $.

The JSON Object is

{"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}]}

Made it short, but the json is valid. I checked in multiple sites.

My Classes are

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;
}}

The code block where Im reading the json is

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();

I do get the required stuff when I comment out the code from reader.beginObject to reader.endObject and uncomment the commented part.

But I want to know why the error is being caused. Did I make a mistake somewhere?

You need to define the ranking class using the annotation. Something like this:

public class Ranking{
   @SerializedNme("hero_id")
   private int hero_id;

   @SerialixedName("ranking")
   private Ranks[] ranks;
}

you need to do the following:

 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();

Hope this helps, it worked for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM