无法反序列化JSON对象

时间:2011-10-13 11:28:59

标签: android json parsing

我正在尝试使用我的Android应用中的GSON解析以下JSON字符串:

{"Links":[{"Name":"Facebook","URL":"http://www.facebook.com/"},{"Name":"Twitter","URL":"http://twitter.com/"},{"Name":"Last FM","URL":"http://www.last.fm/"},{"Name":"Hyves","URL":"http://hyves.nl"},{"Name":"My Space","URL":"http://www.myspace.com/"},{"Name":"YouTube","URL":"http://www.youtube.com/"}]}

这样做时,GSON给了我以下例外:

10-13 11:09:23.103: DEBUG/Error:(752): The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@44e9e430 failed to deserialize json object 
{"Links":[{"Name":"Facebook","URL":"http://www.facebook.com/"},{"Name":"Twitter","URL":"http://twitter.com/"},{"Name":"Last FM","URL":"http://www.last.fm/"},{"Name":"Hyves","URL":"http://hyves.nl"},{"Name":"My Space","URL":"http://www.myspace.com/"},{"Name":"YouTube","URL":"http://www.youtube.com/"}]} 
given the type java.util.List<com.sander.app.json.links.Links>

现在我是JSON的新手,所以我很确定我一定做错了。

我正在使用此方法来解析我的JSON:

       WebService webService = new WebService("http://*********/GetLinksData");

       //Pass the parameters 
       Map<String, String> params = new HashMap<String, String>();
       params.put("iAppID", "59");
       params.put("iFormID", "461");

       //Get JSON response from server the "" are where the method name would normally go if needed example
       // webService.webGet("getMoreAllerts", params);
       String response = webService.webGet("", params);
       System.out.println("Returns: "+response);

       try
       {
           //Parse Response into our object
           Type collectionType = new TypeToken<List<Links>>(){}.getType();
           List<Links> alrt = new Gson().fromJson(response, collectionType);


       }
       catch(Exception e)
       {
           Log.d("Error: ", e.getMessage());
       }
}

这是我的Links类:

public class Links {


public String Name;


public String URL;

public Links(String name, String URL){
    this.Name = name;
    this.URL = URL;
}

@Override
public String toString(){
    return "Name: " + Name + "URL: " + URL;
}}

我怎样才能解决这个问题?我已经被困在这两天了,即使我想学习如何自己解决这个问题,我也没有选择。

问候,

桑德

=============================================== ====

在Raunak的帮助下修复:

public class LinkStorer {     public Link Links [];

public Link[] getLinks(){
    return Links;
}

public Link getSingleLink(int i){
    return Links[i];
}

public static class Link {
    public String Name;
    public String URL;

    public String getName() {
        return Name;
    }

    public String getURL() {
        return URL;
    }
}}

调用JSON对象:

LinkStorer collection = new Gson().fromJson(response, LinkStorer.class);
           for(int i=0; i < collection.Links.length; i++){
               System.out.println(collection.getSingleLink(i).getName());

3 个答案:

答案 0 :(得分:1)

当您定义JSON输入的格式时可能存在问题。您的程序需要以下JSON。

[{"Name":"Facebook","URL":"http://www.facebook.com/"},{"Name":"Twitter","URL":"http://twitter.com/"},{"Name":"Last FM","URL":"http://www.last.fm/"},{"Name":"Hyves","URL":"http://hyves.nl"},{"Name":"My Space","URL":"http://www.myspace.com/"},{"Name":"YouTube","URL":"http://www.youtube.com/"}]

尝试创建另一个POJO

public class LinksSearchResult {

private List<Links> links; 

 public List<Links> getLinks() {
    return links;
 }

}

并像这样使用fromJSON

LinksSearchResult links = new Gson()。fromJson(response,collectionType);

抱歉,但此时我无法正常试试。

答案 1 :(得分:1)

关于为链接创建单独的类,你有正确的想法,但它应该看起来像这样

public class Type {
    public Link Links[];

    public static class Link {
        public String Name;
        public String URL;

        public String getName() {
            return Name;
        }

        public String getURL() {
            return URL;
        }
    }
}

然后,您可以将您的json字符串转换为java对象,如下所示:

Type collection = new Gson().fromJson(response, Type.class);

答案 2 :(得分:0)

也许GSON只能将JSON对象反序列化为标准Java类型?在您的情况下,它可能是List<HashMap<String, List<String>>>之类的东西。完成此操作后,您可以遍历本机Java对象以生成自定义对象。