问题描述
我想使用以下 JSON 创建一个对象清单.但我认为未创建 Arraylist 类别.我在创建对象时调试控制台进入bucle没有异常:后台部分并发标记清除GC释放165848(5MB)AllocSpace对象,144(1852KB)LOS对象,22%空闲,55MB/71MB,暂停5.343ms总共 67.660 毫秒
{"type_check":"CAB","description":"简易舱","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"}]}
以及以下类:
清单.class
公共类清单{私有字符串类型检查;私人字符串描述;私有 ArrayList类别;}
类别.class
公共类类别{私人 int category_id;私人字符串描述;
}
创建对象:
Gson gson = new GsonBuilder().create();清单检查 = gson.fromJson(checklist.toString(), Checklist.class);
解析json时必须指定arraylist??感谢,并有一个愉快的一天.
下面的代码就是 json 的样子
[{"type_check": "CAB_0","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_1","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_2","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_3","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_4","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_5","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]},{"type_check": "CAB_6","description": "简单的小屋",类别":[{"category_id": 0,描述":描述_0"},{"category_id": 1,描述":描述_1"},{category_id":2,描述":描述_2"},{"category_id": 3,描述":描述_3"},{"category_id": 4,描述":描述_4"},{"category_id": 5,描述":描述_5"},{"category_id": 6,描述":描述_6"}]}]
Checklist.Java 代码在这里
公共类清单{私有字符串类型检查;私人字符串描述;私有 ArrayList类别;公共字符串 getType_check() {返回类型检查;}public void setType_check(String type_check) {this.type_check = type_check;}公共字符串 getDescription() {退货说明;}public void setDescription(字符串描述){this.description = 描述;}公共 ArrayList获取类别(){退货类别;}public void setCategories(ArrayListcategories) {this.categories = 类别;}}
Category.java 代码在这里
公共类类别{私人 int category_id;私人字符串描述;公共 int getCategory_id() {返回category_id;}public void setCategory_id(int category_id) {this.category_id = category_id;}公共字符串 getDescription() {退货说明;}public void setDescription(字符串描述){this.description = 描述;}}
TestCode.Java 代码在这里
import java.util.ArrayList;导入 com.google.gson.Gson;导入 com.google.gson.reflect.TypeToken;公共类测试代码 {公共静态无效主(字符串 [] args){ArrayListlist = new ArrayList();list = createList();String stringJson = (new Gson()).toJson(list);System.out.println("##### JSON 来自对象#######");System.out.println(stringJson);System.out.println("###### 将 JSON 转换为对象 ######");ArrayListlist2 = new ArrayList();list2 = (new Gson()).fromJson(stringJson,new TypeToken() {}.getType());System.out.println("####### print out put ######");for (int i = 0; i < list2.size(); i++) {清单清单 = list2.get(i);System.out.println(checklist.getType_check() + "");System.out.println(checklist.getDescription() + "");ArrayList类别 = checklist.getCategories();for (int j = 0; j 创建列表(){//TODO 自动生成的方法存根ArrayListlist = new ArrayList();for (int i = 0; i
I want to create a object Checklist with the following JSON. But I think the Arraylist categories isn't created. I do not have an exception the debug console enter into bucle when create the object : " Background partial concurrent mark sweep GC freed 165848(5MB) AllocSpace objects, 144(1852KB) LOS objects, 22% free, 55MB/71MB, paused 5.343ms total 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"}]}
And the following classes:
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;
}
Creating the object:
Gson gson = new GsonBuilder().create();
Checklist check = gson.fromJson(checklist.toString(), Checklist.class);
I must specify the arraylist when I parse the json?? Thanks and have a nice day.
this is how the json would look like with following code
[
{
"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"
}
]
}, { "type_check": "CAB_1", "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" } ] }, { "type_check": "CAB_2", "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" } ] }, { "type_check": "CAB_3", "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" } ] }, { "type_check": "CAB_4", "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" } ] }, { "type_check": "CAB_5", "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" } ] }, { "type_check": "CAB_6", "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" } ] }]
Checklist.Java code is here
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 code is here
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 code is here
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;
}
}
这篇关于json 到带有 gson 的 java 对象(对象嵌套在 arraylist 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!