我想用以下JSON创建一个对象Checklist。但我认为没有创建Arraylist类别。我没有异常调试控制台在创建对象时进入bucle:“背景部分并发标记扫描GC释放165848(5MB)AllocSpace对象,144(1852KB)LOS对象,22%免费,55MB / 71MB,暂停5.343ms总共67.660ms“
{"type_check":"CAB","description":"simple cabin","categories":[{"category_id":"3","description":"Confort"},{"category_id":"4","description":"Servicios"},{"category_id":"5","description":"Alimentación"},{"category_id":"6","description":"Personal"},{"category_id":"7","description":"Instalaciones"}]}
以下课程:
Checklist.class
public class Checklist {
private String type_check;
private String description;
private ArrayList<Category> categories;}
Category.class
public class Category {
private int category_id;
private String description;
}
创建对象:
Gson gson = new GsonBuilder().create();
Checklist check = gson.fromJson(checklist.toString(), Checklist.class);
我解析json时必须指定arraylist吗?感谢,并有一个愉快的一天。
答案 0 :(得分:1)
以下代码
是json的样子[
{
"type_check": "CAB_0",
"description": "simple cabin",
"categories": [
{
"category_id": 0,
"description": "Description_0"
},
{
"category_id": 1,
"description": "Description_1"
},
{
"category_id": 2,
"description": "Description_2"
},
{
"category_id": 3,
"description": "Description_3"
},
{
"category_id": 4,
"description": "Description_4"
},
{
"category_id": 5,
"description": "Description_5"
},
{
"category_id": 6,
"description": "Description_6"
}
]
}, { &#34; type_check&#34;:&#34; CAB_1&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] }, { &#34; type_check&#34;:&#34; CAB_2&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] }, { &#34; type_check&#34;:&#34; CAB_3&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] }, { &#34; type_check&#34;:&#34; CAB_4&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] }, { &#34; type_check&#34;:&#34; CAB_5&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] }, { &#34; type_check&#34;:&#34; CAB_6&#34;, &#34;描述&#34;:&#34;简单的小屋&#34;, &#34;类别&#34;:[ { &#34; category_id&#34;:0, &#34;描述&#34;:&#34; Description_0&#34; }, { &#34; category_id&#34;:1, &#34;描述&#34;:&#34; Description_1&#34; }, { &#34; category_id&#34;:2, &#34;描述&#34;:&#34; Description_2&#34; }, { &#34; category_id&#34;:3, &#34;描述&#34;:&#34; Description_3&#34; }, { &#34; category_id&#34;:4, &#34;描述&#34;:&#34; Description_4&#34; }, { &#34; category_id&#34;:5, &#34;描述&#34;:&#34; Description_5&#34; }, { &#34; category_id&#34;:6, &#34;描述&#34;:&#34; Description_6&#34; } ] } ]
Checklist.Java代码在这里
public class Checklist {
private String type_check;
private String description;
private ArrayList<Category> categories;
public String getType_check() {
return type_check;
}
public void setType_check(String type_check) {
this.type_check = type_check;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<Category> getCategories() {
return categories;
}
public void setCategories(ArrayList<Category> categories) {
this.categories = categories;
}
}
Category.java代码在这里
public class Category {
private int category_id;
private String description;
public int getCategory_id() {
return category_id;
}
public void setCategory_id(int category_id) {
this.category_id = category_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
TestCode.Java代码在这里
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class TestCode {
public static void main(String[] args) {
ArrayList<Checklist> list = new ArrayList<Checklist>();
list = createList();
String stringJson = (new Gson()).toJson(list);
System.out.println("##### JSON from a object #######");
System.out.println(stringJson);
System.out.println("###### Convert JSON to object ######");
ArrayList<Checklist> list2 = new ArrayList<Checklist>();
list2 = (new Gson()).fromJson(stringJson,
new TypeToken<ArrayList<Checklist>>() {
}.getType());
System.out.println("####### print out put ######");
for (int i = 0; i < list2.size(); i++) {
Checklist checklist = list2.get(i);
System.out.println(checklist.getType_check() + "");
System.out.println(checklist.getDescription() + "");
ArrayList<Category> categorys = checklist.getCategories();
for (int j = 0; j < categorys.size(); j++) {
Category category = categorys.get(j);
System.out.println(category.getCategory_id() + "");
System.out.println(category.getDescription() + "");
}
}
}
private static ArrayList<Checklist> createList() {
// TODO Auto-generated method stub
ArrayList<Checklist> list = new ArrayList<Checklist>();
for (int i = 0; i < 7; i++) {
Checklist checklist = new Checklist();
checklist.setType_check("CAB_" + i);
checklist.setDescription("simple cabin");
ArrayList<Category> categorys = new ArrayList<Category>();
for (int j = 0; j < 7; j++) {
Category category = new Category();
category.setCategory_id(j);
category.setDescription("Description_" + j);
categorys.add(category);
}
checklist.setCategories(categorys);
list.add(checklist);
}
return list;
}
}