user1796624
user1796624

Reputation: 4007

gson array parsing

how to parse

{
    "urls": [
        "http://www.url.com",
        "http://www.url.com",
        "http://www.url.com",
        "http://www.url.com",
        "http://www.url.com"
    ]
}

using gson? Ill always parse the string in the format :

{
        "urls": [
            x:"http://www.url.com",
            x:"http://www.url.com",
            x: "http://www.url.com",
            x: "http://www.url.com",
            x: "http://www.url.com"
        ]
    }

how to parse the string if the name of the variable (x in this case) is not included?

Upvotes: 0

Views: 176

Answers (1)

iagreen
iagreen

Reputation: 31996

It can be parsed as an Object that has only one field urls which is of type String[]. In other words,

class UrlList {
    String[] urls;
}

Assuming you put the text in a String named gsonText, you can parse it with

Gson gson = new Gson();
UrlList uList = gson.fromJson(gsonText, UrlList.class); 

Upvotes: 1

Related Questions