这是我使用Gson进行序列化的第一种方法.我像这样回复了我的Android应用程序的facebook响应
我的Json:
{"data": [ { "pic_square": "https://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg", "uID": "10202xxx852765", "name": "Mister X" }, { "pic_square": "https://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg", "uID": "10202xxx852765", "name": "Mister X" } ]} try { final GsonBuilder builder = new GsonBuilder(); final Gson gson = builder.create(); JsONObject data= response.getGraphObject().getInnerjsonObject(); FacebookResponses facebookResponses= gson.fromJson(data.toString(),FacebookResponses.class); //exception here Log.i(TAG, "Result: " + facebookResponses.toString()); } catch (JsonSyntaxException e) { e.printstacktrace();
}
我的课
public class FacebookResponses implements Serializable { private static final long serialVersionUID = 1L; @Serializedname("data"); private FacebookRisp[] data;}class FacebookRisp implements Serializable { private static final long serialVersionUID = 1L; @Serializedname("pic_square") private String[] pic_square; @Serializedname("uID") private String[] uID; @Serializedname("name") private String[] name; public String[] getPic_square() { return pic_square; } public voID setPic_square(String[] pic_square) { this.pic_square = pic_square; } public String[] getUID() { return uID; } public voID setUID(String[] uID) { this.uID = uID; } public String[] getname() { return name; } public voID setname(String[] name) { this.name = name; } }
我明白了
com.Google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY但在第1行第118列为STRING
更新:
我修改了爱琴海的答案,问题是[]
@Serializedname("pic_square")private String**[]** pic_square; //ex here and others
解决方法:
将您的FacebookResponses类更改为:
private class FacebookResponses { private Data[] data;}private class Data { @Serializedname("pic_square") private String picSquare; private String uID; private String name;}
编辑:因为您的Json响应的结构如下所示:
总结以上是内存溢出为你收集整理的com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但是STRING全部内容,希望文章能够帮你解决com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但是STRING所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)