开发者

How do I decode a list of string pairs with GSON?

I have a list of categories coming back from a web request, in JSON format. An example might be:

"categories":[["Descriptive Cat 1 Name","cat1label"]]

How would that be represented in the object? I currently have an obj开发者_运维百科ect called Category, and am using it like:

private List<Category> categories;

The Category object looks something like:

class Category {
   private String descrName;
   private String label;
    .. getters and setters..
}

When attempting to decode with GSON I get this eror:

01-27 21:44:46.149: ERROR/AndroidRuntime(843): com.google.gson.JsonParseException: Expecting array but found object: Category@437d1ff8

Any suggestions? I would also be OK having those come back as a map, although instead of K,V in the JSON results they would be V,K, could it be mapped that way?

It does work if I ditch the Category object all together, and just map it as:

private List<List<String,String>> categories;

But is there a better way to represent that data?

Nick


As @Dante617 correctly pointed, your JSON representation is not correct. The correct representation is

{
    "categories": [
                   {"descrName":"Descriptive Cat Name 1", "label": "cat1Label"},
                   {"descrName":"Descriptive Cat Name 2", "label": "cat2Label"}
                  ]
}

Now, this can be thought of as a map of "categories" title and list of Category objects. So, the Java object, that maps it, will be Map<String, List<Category>>

If you somehow reformat your string correctly like the one above. Here is how you would parse.

    String categories = "{\"Categories\":[{\"descrName\":\"Descriptive Cat 1 Name\",\"label\":\"cat1label\"}, {\"descrName\":\"Descriptive Cat 2 Name\",\"label\":\"cat2label\"}]}";
    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, List<Category>>>() {}.getType();
    Map<String, List<Category>> l = gson.fromJson(categories, type);
    System.out.println("l: " + l);

If your Java object looks like this

public class Category {
    private String descrName;
    private String label;
    //no need of getters and setters. Reflection, baby! :)

    @Override
    public String toString() {
        return "<Name:"+descrName+", label:"+label+">";
    }
}

The output will show like this

 l: {Categories=[<Name:Descriptive Cat 1 Name, label:cat1label>, <Name:Descriptive Cat 2 Name, label:cat2label>]}


I'm not familiar with GSON, but I'm not sure how the application could map the strings to the fields in your object. It seems like you want a JSON structure more like:

"categories": [
               {"descrName":"Descriptive Cat Name 1", "label": "cat1Label"},
               {"descrName":"Descriptive Cat Name 2", "label": "cat2Label"}
              ]

That might help in being able to dynamically create the Java objects.


In contrary to what others think, that's not invalid JSON. The [[]] is just a two-dimensional array. In Java terms, it maps as follows:

String json = "{\"categories\":[[\"Descriptive Cat 1 Name\",\"cat1label\"]]}";
Map<String, String[][]> map = new Gson().fromJson(json, new TypeToken<Map<String, String[][]>>(){}.getType());
// ...

or

String json = "{\"categories\":[[\"Descriptive Cat 1 Name\",\"cat1label\"]]}";
Map<String, List<List<String>>> map = new Gson().fromJson(json, new TypeToken<Map<String, List<List<String>>>>(){}.getType());
// ...


Thanks for the input folks. Unfortunately this is a public web service I'm utilizing (the Yelp v2 API) and I can't change the format of the data they are returning. This is what I stuck with for now which is working fine:

private List<List<String>> categories;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜