我試圖解析使用GSON以下JSON字符串在我的Android應用:未能反序列化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 [email protected]30 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());
}
}
這是我的鏈接類:
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的幫助修正:
公共類LinkStorer { 公共鏈接,友情鏈接[];
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());
你,我的好人,是一個現場救星! 我的最終修復是在開始帖子中。 –
我的壞,一般來說還是新的。以爲我點擊它,但猜測我沒有。不過謝謝你! –