G s o n c a n n o t d e serialize {"b":"abc"} into an instance of B since the class B is an inner class. if it was defined as static class B then Gson would have been able to deserialize the string. Another solution is to write a custom instance creator for B.
Java代码
public class InstanceCreatorForB implements InstanceCreator<A.B> {
private final A a;
public InstanceCreatorForB(A a) {
this.a = a;
}
public A.B createInstance(Type type) {
return a.new B();
}
}
The above is possible, but not recommended.
数组例子:
Java代码
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
(Serialization)
gson.toJson(ints); ==> prints [1,2,3,4,5]
gson.toJson(strings); ==> prints ["abc", "def", "ghi"]
(Deserialization)
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
==> ints2 will be same as ints
We also support multi-dimensional arrays, with arbitrarily complex element types
综合实例1:
Java代码
public class ExampleBean {
private String name;
private String id;
private int age;
private boolean isOk;
public ExampleBean(String name, String id, int age, boolean isOk) {
super();
this.name = name;
this.id = id;
this.age = age;
this.isOk = isOk;
}
public ExampleBean() {
}
//setter和getter方法
}
测试:
Java代码
public static void main(String[] args) {
Gson gson = new Gson();
List<ExampleBean> list = new ArrayList<ExampleBean>();
for (int i = 0; i < 5; i++) {
String name = "xxlong" + i;
int age = 20 + i;
ExampleBean bean = new ExampleBean(name, i + "", age);
list.add(bean);
}
Type listType = new TypeToken<List<ExampleBean>>() {
}.getType();
//将list转化成json字符串
String json = gson.toJson(list);
System.out.println(json);
//将json字符串还原成list
List<ExampleBean> list2 = gson.fromJson(json, listType);
}
输出如下:[{"name":"xxlong0","id":"0","age":20,"isOk":false},{"name":"xxlong1","id":"1","age":21,"isOk":false},{"name":"xxlong2","id":"2","age":22,"isOk":false},{"name":"xxlong3","id":"3","age":23,"isOk":false},{"name":"xxlong4","id":"4","age":24,"isOk":false}]
综合实例2:
需求:想将字符串{'tableName' :'ys_index_y','year': '2008','params':'[z_expense,z_expense_profit,z_main_margin]','isOperAll':'false','copyToYear':''}还原成对象OpeerConditions,OpeerConditions对象代码如下所示:
Java代码
public class OperConditions {
private String tableName;
private String year;
private String[] params;
private boolean isOperALl;
private String copyToYear;
public OperConditions() {
}
public OperConditions(String tableName, String year, String[] params,
boolean isOperALl, String copyToYear) {
super();
this.tableName = tableName;
this.year = year;
this.params = params;
this.setOperALl(isOperALl);
this.copyToYear = copyToYear;
}
//getter和setter方法
}
因为OperConditions中有属性params,它是一个数组,所以无论是用json lib还是gson都不能直接将上面的字符串还原成OperCondtions对象,可以直接将params分离出来,单独处理,我这里选用此种方法来处理:
json-lib代码如下:
Java代码
public static void main(String[] args) {
String json = "{'tableName' :'ys_index_y','year': '2008','isOperAll':'false','copyToYear':''}";
JSONObject jsonObj = JSONObject.fromObject( json );
OperConditions conditions = (OperConditions) JSONObject.toBean( jsonObj, OperConditions.class );
System.out.println(conditions.isOperALl() == false); //==>输出为true
String json1 = "['z_expense','z_expense_profit','z_main_margin']";
JSONArray jsonArray = JSONArray.fromObject(json1);
//List<String> list = jsonArray.toList(jsonArray); //这个方法也可以
List<String> list = jsonArray.toList(jsonArray,String.class);
conditions.setParams(list.toArray(new String[0]));
System.out.println(conditions.getParams()[0]); //==>输出为z_expense
}
因为JSONArray的toArray()方法返回的是一个Object[]数组,所以先将它转化成list,再转化到String数组。
当然由JSONArray转化成list时也可以使用subList方法,如下所示:
Java代码
List<String> list = jsonArray.subList(0, jsonArray.size());
或者可以直接使用JSONArray的iterator() 方法迭代它本身直接得到需要的String数组。
如果使用Gson来完成这一需求,个人感觉更简单,代码如下所示:
Java代码
public static void main(String[] args) {
String json = "{'tableName' :'ys_index_y','year': '2008','isOperAll':'false','copyToYear':''}";
Gson gson = new Gson();
OperConditions conditions = gson.fromJson(json, OperConditions.class);
System.out.println(conditions.isOperALl() == false); // ==>输出为true
String json1 = "['z_expense','z_expense_profit','z_main_margin']";
String[] params = gson.fromJson(json1,String[].class);
conditions.setParams(params);
System.out.println(conditions.getParams()[0]); // ==>输出为z_expense
}
Gson可以直接转化成String[]数组,同时转化OperConditions时也比json-lib简单。