How to read JSON variable that use number for its name, with GSON

后端 未结 4 1424
暗喜
暗喜 2021-01-16 12:06

First, im a beginner in GSON so please bear with me.

I tried to read a JSON from this url :

https://gdata.youtube.com/feeds/api/videos?author=radityad

相关标签:
4条回答
  • 2021-01-16 12:42

    You need define several classes first of all:

    MyGson

    public class MyGson {
    private String apiVersion;
    private Data data;
    
    public Data getData() {
        return data;
    }
    }
    

    Data

    public class Data {
    private String updated;
    private int totalItems = 0;
    private int startIndex = 0;
    private int itemsPerPage = 0;
    private  List<Item> items;
    
    public List<Item> getItems() {
        return items;
    }
    }
    

    Item

     public class Item {
    private String id;
    private String uploaded;
    private String updated;
    private String uploader;
    private String category;
    private String title;
    private String description;
    private Map<Integer, String>  content;
    
    public Map<Integer, String> getContent() {
        return content;
    }
    }
    

    Take a look, your content is map where key is 1,2,3,4,5,6 ....

    You can define Map<String, String> content but since all your keys are integers..

    So now you can extract any value you want:

    Launcher

     ....
     Gson gson = new Gson();
    
        MyGson myGson = gson.fromJson(str, MyGson.class);
    
        List<Item> items = myGson.getData().getItems();
    
        if(items.size()>0){
            Item item = items.get(0);
    
            String myStr = item.getContent().get(1);
    
            System.out.println(myStr);
        }
    

    Output:

    rtsp://r6---sn-cg07lue6.c.youtube.com/CiILENy73wIaGQl1cubZZSUSXxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
    
    0 讨论(0)
  • 2021-01-16 12:51

    Java languge does not allow variable naming like this.

    A relevant exceprt is quoted herein, please see the link below for details, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

    Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows:

    1. Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

    2. Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

    3. If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

    0 讨论(0)
  • 2021-01-16 13:02

    It's old but maybe someone needs it still...

    To serialize properties which name is just Integer just make model class like:

    Json:

    {
      "name": "foo",
      "1": "value one",
      "2": "value two",
      "3": "value three"
    }
    

    Java:

    import com.google.gson.annotations.SerializedName;
    
    public class Foo {
    
       private String name;
    
       @SerializedName("1")
       private String one;
    
       @SerializedName("2")
       private String two;
    
       @SerializedName("3")
       private String three;
    
       // standard getter & setters bellow...
    
    }
    
    0 讨论(0)
  • 2021-01-16 13:05

    Taking into account that the content JSON object looks like this:

    "content": {
        "1": "someLink",
        "5": "someOtherLink",
        ...
    }
    

    The best way to parse that JSON object is as a Map (see Map documentation), so you just need to add an attribute to your Item class like this:

    private Map<Integer, String> content;
    

    Basically a Map is an object containing pairs of key - value, in your case the keys are Integer and the values are String.

    So then you can access your link looking for the key of the value you want to retrieve, in the case of the first link, it's just:

    String someLink = content.get(new Integer(1));
    

    Note that doing it this way you can have different numbers for the links. Now you have 1, 5 and 6. But you could have any integers and an arbitrary number of links...

    0 讨论(0)
提交回复
热议问题