Failure to deserialize JSON object
I'm trying to parse the following JSON string using GSON in my Android app:
{"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/"}]}
When doing this, GSON gives me the following exception:
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>
Now I am a complete novice to JSON, so I'm pretty sure I must be doing something wrong.
I'm using this method to parse my 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());
}
}
And this is my Links class:
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;
}}
How might I be able to fix this problem? I have been stuck on this for two da开发者_如何学Goys now, and even though I want to learn how to fix things like this by myself, I'm running out of options.
Regards,
Sander
===================================================
Fixed with the help of 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;
}
}}
Call for JSON object:
LinkStorer collection = new Gson().fromJson(response, LinkStorer.class);
for(int i=0; i < collection.Links.length; i++){
System.out.println(collection.getSingleLink(i).getName());
You have the right idea with regards to making a separate class for links, though, it should look something like this
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;
}
}
}
You can then convert your json string into java object like this:
Type collection = new Gson().fromJson(response, Type.class);
Maybe the problem that when you define the format of JSON input. Your program expects the following 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/"}]
Try to create another POJO
public class LinksSearchResult {
private List<Links> links;
public List<Links> getLinks() {
return links;
}
}
And use fromJSON like this
LinksSearchResult links = new Gson().fromJson(response, collectionType);
Sorry but at this moment i'm unable to try this properly.
Perhaps GSON
can only deserialize a JSON
object to a standard Java type? In your case, it may be some thing like List<HashMap<String, List<String>>>
. Once you have this, you can iterate through the native Java object to generate your custom object.
精彩评论