Gson - 放入并获取组合对象的ArrayList [英] Gson - putting and getting ArrayList of composite objects
问题描述
我有类 NameAndPostion
,其中我将存储用户的名称和位置。我创建了 ArrayList
NameAndPosition
。
的ArrayList< NameAndPosition> LocSenders = new ArrayList< NameAndPosition>();
现在我已将 LocSenders
转换为JSON字符串通过使用Gson库
Gson gson = new Gson();
String json = gson.toJson(LocSenders);
我做了这个转换来保存 LocSenders
in Shared Pref。
现在我想要检索'LocSenders'中使用Gson JSON转换存储的所有对象。
我想使用Gson库做这项工作。
我没有得到Gsons方法 gson.fromJson(json ,classOfT)
。所以如何正确地做到这一点?
NameAndPostion.java
public class NameAndPosition {
private String name =;
私人LatLng位置=空;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public LatLng getPosition(){
return position;
}
public void setPosition(LatLng position){
this.position = position;
}
}
在此先感谢。
下面是使用gson转换为JSON和使用JSON的例子。
//转换为JSON
System.out.println(gson.toJson(employee));
//转换为java对象
System.out.println({'id':1,'firstName':'Lokesh','lastName':'Gupta ','roles':['ADMIN','MANAGER'],'birthDate':'17/06/2014'}
,Employee.class));
反序列化集合
列表与LT;员工> yourClassList = new Gson()。fromJson(jsonArray,Employee.class);
用于泛型Collection。
类型listType = new TypeToken< ArrayList< YourClass>>(){
} .getType();
列表< YourClass> yourClassList = new Gson()。fromJson(jsonArray,listType);
您也可以参考下面的链接。
Google Gson - 反序列化列表< class>目的? (泛型)
I have class NameAndPostion
in which I am going to store name and position of users. And I have created ArrayList
of objects of NameAndPosition
.
ArrayList<NameAndPosition> LocSenders = new ArrayList<NameAndPosition>();
Now I have converted LocSenders
into JSON string by using Gson library
Gson gson = new Gson();
String json = gson.toJson(LocSenders);
I did this conversion to save LocSenders
in Shared Pref.
Now I want to retrieve all objects in 'LocSenders' which I have stored using Gson JSON conversion.
I want to do this work using Gson library.
I am not getting the Gsons method gson.fromJson(json, classOfT)
. So how to do that properly?
NameAndPostion.java
public class NameAndPosition {
private String name = "";
private LatLng position = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LatLng getPosition() {
return position;
}
public void setPosition(LatLng position) {
this.position = position;
}
}
Thanks in advance.
Here is example of convert to JSON and from JSON using gson.
//Convert to JSON
System.out.println(gson.toJson(employee));
//Convert to java objects
System.out.println(gson.fromJson("{'id':1,'firstName':'Lokesh','lastName':'Gupta','roles':['ADMIN','MANAGER'],'birthDate':'17/06/2014'}"
, Employee.class));
Deserialize collection
List<Employee> yourClassList = new Gson().fromJson(jsonArray, Employee.class);
For generic Collection.
Type listType = new TypeToken<ArrayList<YourClass>>() {
}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
you can also refere below link.
Google Gson - deserialize list<class> object? (generic type)
这篇关于Gson - 放入并获取组合对象的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!