[{ "ID": 1,"name": "Australia","Active": true},{ "ID": 3,"name": "KIEv",{ "ID": 4,"name": "South Africa",{ "ID": 5,"name": "Stockholm",{ "ID": 6,"name": "Paris",{ "ID": 7,"name": "Moscow",{ "ID": 8,"name": "New York City",{ "ID": 9,"name": "Germany",{ "ID": 10,"name": "copenhagen",{ "ID": 11,"name": "Amsterdam","Active": true}]
这是将被使用的对象
public class MyBranch extends Entity {public MyBranch () { super();}public MyBranch (int ID,String name,String isActive) { super(); _ID = ID; _name = name; _Active = isActive;}@Column(name = "ID",primaryKey = true)public int _ID;public String _name;public String _Active;}Gson gson = new Gson();Type t = new Typetoken<List<MyBranch >>() {}.getType(); List<MyBranch > List = (List<MyBranch >) gson.fromJson(Json,t);
构造的列表,它有10个对象,但问题是对象的所有数据成员都是null,我不会对此有什么不妥.实体是OrmDroID的实体类.
解决方法 MyBranch类中字段的名称与Json中的字段不匹配,因此您必须使用Serializedname批注.import com.Google.gson.annotations.Serializedname;public class MyBranch extends Entity { public MyBranch () { super(); } public MyBranch (int ID,String isActive) { super(); _ID = ID; _name = name; _Active = isActive; } @Column(name = "ID",primaryKey = true) @Serializedname("ID") public int _ID; @Serializedname("name") public String _name; @Serializedname("Active") public String _Active;}
编辑:
您还可以通过简单重命名MyBranch字段来避免使用Serializedname注释:
import com.Google.gson.annotations.Serializedname;public class MyBranch extends Entity { public MyBranch () { super(); } public MyBranch (int ID,String isActive) { super(); ID = ID; name = name; Active = isActive; } @Column(name = "ID",primaryKey = true) public int ID; public String name; public String Active;}总结
以上是内存溢出为你收集整理的android – 使用gson解析Json返回null对象全部内容,希望文章能够帮你解决android – 使用gson解析Json返回null对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)