Skip to content Skip to sidebar Skip to footer

How To Read Json Variable That Use Number For Its Name, With Gson

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=radityadika&v=2&alt=jsonc I s

Solution 1:

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;

publicclassFoo {

   privateString name;

   @SerializedName("1")
   privateString one;

   @SerializedName("2")
   privateString two;

   @SerializedName("3")
   privateString three;

   // standard getter & setters bellow...

}

Solution 2:

You need define several classes first of all:

MyGson

publicclassMyGson{
private String apiVersion;
private Data data;

public Data getData() {
    returndata;
}
}

Data

publicclassData {
private String updated;
privateint totalItems = 0;
privateint startIndex = 0;
privateint itemsPerPage = 0;
private  List<Item> items;

public List<Item> getItems() {
    return items;
}
}

Item

publicclassItem {
privateString id;
privateString uploaded;
privateString updated;
privateString uploader;
privateString category;
privateString title;
privateString description;
privateMap<Integer, String>  content;

publicMap<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

Solution 3:

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(newInteger(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...

Solution 4:

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.

Post a Comment for "How To Read Json Variable That Use Number For Its Name, With Gson"