Json用gson解析返回空对象 [英] Json parsing with gson returns null object
问题描述
[
{
ID:1,
姓名:澳大利亚,
活跃:真实
},
{
ID:3,
姓名:基辅,
有效:真
},
{
ID:4,
姓名:南非,
活跃:真实
},
{
ID:5,
姓名:斯德哥尔摩,
有效:真
},
{
ID:6,
姓名:巴黎,
有效:真
,
{
ID:7,
姓名:莫斯科,
有效:真
},
{
ID:8,
姓名:纽约市,
有效:真
},
{
ID :9,
姓名:德国,
有效:真
},
{
ID:10,
姓名:哥本哈根,
有效:真实
},
{
ID:11,
姓名:阿姆斯特丹,
活跃:真实
}
]
这是将要使用的Object
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();
键入t = new TypeToken< List< MyBranch>>(){} .getType();
列表< MyBranch> list =(List< MyBranch>)gson.fromJson(json,t);
列表构造它有10个对象,但问题是所有对象的数据成员都是null,我不知道这有什么问题。该实体是OrmDroid的实体类。
您的 MyBranch
class与 json
中的字段不匹配,因此您必须使用 SerializedName
注释。
import com.google.gson.annotations.SerializedName;
$ b 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)
@SerializedName(ID)
public int _ID;
@SerializedName(Name)
public String _Name;
@SerializedName(Active)
public String _Active;
}
编辑:
您也可以通过简单重命名 MyBranch
字段避免使用 SerializedName
注释:
import com.google.gson.annotations.SerializedName;
$ b public class MyBranch extends Entity {
public MyBranch(){
super();
}
public MyBranch(int id,String name,String isActive){
super();
ID = id;
名称=名称;
Active = isActive;
@Column(name =id,primaryKey = true)
public int ID;
public String Name;
public String Active;
}
I am parsing a Json string via gson , this is the Json string
[
{
"ID": 1,
"Name": "Australia",
"Active": true
},
{
"ID": 3,
"Name": "Kiev",
"Active": true
},
{
"ID": 4,
"Name": "South Africa",
"Active": true
},
{
"ID": 5,
"Name": "Stockholm",
"Active": true
},
{
"ID": 6,
"Name": "Paris",
"Active": true
},
{
"ID": 7,
"Name": "Moscow",
"Active": true
},
{
"ID": 8,
"Name": "New York City",
"Active": true
},
{
"ID": 9,
"Name": "Germany",
"Active": true
},
{
"ID": 10,
"Name": "Copenhagen",
"Active": true
},
{
"ID": 11,
"Name": "Amsterdam",
"Active": true
}
]
and this is the Object which is going to be useed
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);
the list constructed and it has 10 object , but the problem is all of object's data members is null, i dont no what's wrong with this. The Entity is OrmDroid's Entity class.
Names of the fields in your MyBranch
class don't match to the fields in your json
so you have to use SerializedName
annotation.
import com.google.gson.annotations.SerializedName;
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)
@SerializedName("ID")
public int _ID;
@SerializedName("Name")
public String _Name;
@SerializedName("Active")
public String _Active;
}
EDIT:
You can also avoid using SerializedName
annotation by simple renaming MyBranch
fields:
import com.google.gson.annotations.SerializedName;
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;
}
这篇关于Json用gson解析返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!