如何使用GSON解码字符串对列表? [英] How do I decode a list of string pairs with GSON?

查看:171
本文介绍了如何使用GSON解码字符串对列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Web请求返回的类别列表,以JSON格式显示。一个例子可能是:
$ b $

 categories:[[Cat1名称描述,cat1label]] 

这会如何表现在对象中?我目前有一个名为Category的对象,并使用它:

  private List< Category>类别; 

Category对象如下所示:

  class Category {
private String descrName;
私人字符串标签;
.. getters and setters ..
}

当试图用GSON我得到这个错误:

lockquote
01-27 21:44:46.149:ERROR / AndroidRuntime(843):com.google.gson。 JsonParseException:期待数组,但找到对象:Category @ 437d1ff8


任何建议?我也可以让那些回来作为地图,虽然不是K,V在JSON结果中,他们会是V,K,它可以这样映射吗?



如果我将Category对象放在一起,并将其映射为:

  private List< List< String,String>>类别; 

但是,有没有更好的方式来表示这些数据?



Nick

解决方案

感谢您的投入。不幸的是,这是一个我正在使用的公共Web服务(Yelp v2 API),我无法更改它们返回的数据格式。这是我现在坚持的工作正常:

 私人列表<列表< String>>类别; 


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

解决方案

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;

这篇关于如何使用GSON解码字符串对列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆