2

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.

4
  • The question isn't clear. Please add exception's stack trace. Commented Mar 25, 2015 at 9:36
  • The app don't throw any exception, the console enter into bucle and that's the message: 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 Commented Mar 25, 2015 at 9:40
  • Hm, it's works fine for me. Are you sure about string in checklist.toString()? Commented Mar 25, 2015 at 9:53
  • Sorry the code works, I deleted an old class Checklist but the class still in the schema project. Sorry , how can I delete the question? Thanks. Commented Mar 25, 2015 at 10:13

1 Answer 1

1

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;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.