如何从json获取带有模型类的类别和子类别

时间:2016-01-19 09:48:06

标签: java android json

我已经从json

下面创建了一个类别的模型类
    {
"currency": {
    "INR": "1.000000000000",
    "USD": "0.015400000000"
},
"category": [{
    "category_id": 3,
    "parent_id": 2,
    "name": "Gifts & Smiles",
    "is_active": 1,
    "position": 1,
    "level": 2,
    "children": []
},
{
    "category_id": 4,
    "parent_id": 2,
    "name": "Send To",
    "is_active": 1,
    "position": 2,
    "level": 2,
    "children": [{
        "category_id": 22,
        "parent_id": 4,
        "name": "India",
        "is_active": 1,
        "position": 1,
        "level": 3,
        "children": [{
            "category_id": 8,
            "parent_id": 22,
            "name": "Agra",
            "is_active": 1,
            "position": 1,
            "level": 4,
            "children": []
        },
        {
            "category_id": 6,
            "parent_id": 22,
            "name": "Ahmedabad",
            "is_active": 1,
            "position": 2,
            "level": 4,
            "children": []
        },
        {
            "category_id": 9,
            "parent_id": 22,
            "name": "Akola",
            "is_active": 1,
            "position": 3,
            "level": 4,
            "children": []
        },
        {
            "category_id": 10,
            "parent_id": 22,
            "name": "Allahabad",
            "is_active": 1,
            "position": 4,
            "level": 4,
            "children": []
        },
        {
            "category_id": 11,
            "parent_id": 22,
            "name": "Aligarh",
            "is_active": 1,
            "position": 5,
            "level": 4,
            "children": []
        },
        {
            "category_id": 12,
            "parent_id": 22,
            "name": "Ambala",
            "is_active": 1,
            "position": 6,
            "level": 4,
            "children": []
        },
        {
            "category_id": 13,
            "parent_id": 22,
            "name": "Amravati",
            "is_active": 1,
            "position": 7,
            "level": 4,
            "children": []
        },
        {
            "category_id": 15,
            "parent_id": 22,
            "name": "Amritsar",
            "is_active": 1,
            "position": 8,
            "level": 4,
            "children": []
        },
        {
            "category_id": 14,
            "parent_id": 22,
            "name": "Aurangabad",
            "is_active": 1,
            "position": 9,
            "level": 4,
            "children": []
        },
        {
            "category_id": 7,
            "parent_id": 22,
            "name": "Bangalore",
            "is_active": 1,
            "position": 10,
            "level": 4,
            "children": []
        },
        {
            "category_id": 16,
            "parent_id": 22,
            "name": "Baroda",
            "is_active": 1,
            "position": 11,
            "level": 4,
            "children": []
        },
        {
            "category_id": 17,
            "parent_id": 22,
            "name": "Bhopal",
            "is_active": 1,
            "position": 12,
            "level": 4,
            "children": []
        },
        {
            "category_id": 18,
            "parent_id": 22,
            "name": "Chandigarh",
            "is_active": 1,
            "position": 13,
            "level": 4,
            "children": []
        },
        {
            "category_id": 19,
            "parent_id": 22,
            "name": "Chennai",
            "is_active": 1,
            "position": 14,
            "level": 4,
            "children": []
        },
        {
            "category_id": 21,
            "parent_id": 22,
            "name": "Delhi",
            "is_active": 1,
            "position": 15,
            "level": 4,
            "children": []
        }]
    },
    {
        "category_id": 23,
        "parent_id": 4,
        "name": "Abroad",
        "is_active": 1,
        "position": 2,
        "level": 3,
        "children": []
    }]
},
{
    "category_id": 20,
    "parent_id": 2,
    "name": "Gift Cards",
    "is_active": 1,
    "position": 3,
    "level": 2,
    "children": []
}]}

这是我从json

获取类别的模型类
public class ModelCategories {

public static ArrayList<String> id = new ArrayList<String>();
public static ArrayList<String> name = new ArrayList<String>();
}

我知道如何解析json,但我的问题是如何在模型类中存储子类别我是这类场景的新手。

所以请帮助我......

1 个答案:

答案 0 :(得分:1)

我不知道你为什么要创建像这个ModelCategories这样的模型。 如果我必须解决这个问题,我将创建一个名为Category的模型:

  public class Category {
       String id;
       String name;
       int position;
       List<Category> subCategories;
  }