简体   繁体   English

使用GSON解析包含对象的数组,该对象包含所需的String

[英]Using GSON to parse an array of objects that contains an object that contains a needed String

To start, here is the item I am trying to parse. 首先,这是我要解析的项目。 Right now, I am trying to obtain the value of the "name" key from this JSON. 现在,我正在尝试从此JSON获取“名称”键的值。 I haven't had trouble using GSON to obtain other necessary variables, but this one is much trickier than those. 使用GSON来获取其他必要的变量我没有遇到麻烦,但是这个变量比那些要难得多。

 "types": [
        {
            "slot": 2,
            "type": {
                "url": "https://www.pokeapi.co/api/v2/type/4/",
                "name": "poison"
            }
        },
        {
            "slot": 1,
            "type": {
                "url": "https://www.pokeapi.co/api/v2/type/12/",
                "name": "grass"
            }
        }
    ]

Here is my code that allowed me to successfully create the treecko object from all of the desired variables besides the array of Types. 这是我的代码,它使我能够从Types数组之外的所有所需变量中成功创建treecko对象。

            Gson gson = new Gson();
            PokedexMemberJava treecko = gson.fromJson(jsonPokemon, PokedexMemberJava.class);

The classes I used to attempt to create the object from the JSON. 我用来尝试通过JSON创建对象的类。 When I attempted to create the object from the JSON, all variables were successfully loaded besides the array of Types, which was created as null. 当我尝试通过JSON创建对象时,除类型数组(已创建为null)外,所有变量都已成功加载。 These other variables were loaded from a different section of JSON, but I included a link to this at the bottom of this post if looking at it is necessary. 这些其他变量是从JSON的不同部分加载的,但是如果需要查看的话,我在这篇文章的底部提供了指向此变量的链接。

public class PokedexMemberJava {
    int id;
    String name;
    int height;
    int weight;
    Types[] type;

public PokedexMemberJava(int id, String name, int height, int weight){
    this.id = id;
    this.name = name;
    this.height = height;
    this.weight = weight;
}


}

public class Types {
    public Type typeObject;
    public int slot;

public Types (Type typeObject, int slot){
    this.typeObject = typeObject;
    this.slot = slot;  
}

}

public class Type {
    public String url;
    public String name;

public Type (String url, String name){
    this.url = url;
    this.name = name;
}

}

I've spent a couple hours trying to figure this out and trying to get the name variable with other libraries like org.json to no avail. 我花了几个小时试图弄清楚这一点,并尝试使用org.json之类的其他库来获取名称变量,但无济于事。 I only included the JSON of the part I was trying to obtain above. 我只在上面包含了我尝试获取的部分的JSON。 If for some reason you want to see what the entire JSON object would look like that I'm trying to parse, you can find it here: https://pokeapi.co/api/v2/pokemon/252/ . 如果出于某种原因您想要查看我尝试解析的整个JSON对象的外观,可以在以下位置找到它: https : //pokeapi.co/api/v2/pokemon/252/ Also, I apologize for any mistakes or poor formatting of this post. 另外,对于本文的任何错误或格式错误,我深表歉意。 It is my first time posting a question on StackOverflow. 这是我第一次在StackOverflow上发布问题。

You have json field name mismatch: 您的JSON字段名称不匹配:

  1. Instead of Types[] types; 代替Types[] types; , you have Types[] type; ,您拥有Types[] type; which does not match types property in json 与json中的types属性不匹配
  2. Instead of Type type; 代替Type type; , you have Type typeObject; ,您有Type typeObject; which does not match type property in json 与json中的type属性不匹配

So you have 2 options: 因此,您有2个选择:

  1. Rename java field to match the json property OR 重命名java字段以匹配json属性或
  2. Use @SerializedName to override the property name: 使用@SerializedName覆盖属性名称:

     public class PokedexMemberJava { @SerializedName("types") Types[] type; ... public class Types { @SerializedName("type") public Type typeObject; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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