将JSon String解析到第n级

时间:2013-11-28 07:59:32

标签: java json gson

你好我有Json String,它的深度达到N级,跟随我的Json String

{
           "id": 11,
           "name": "Release 5",
           "can_modify": true,
           "start_date": null,
           "due_date": null,
           "velocity_start_date": null,
           "capacity":
           {
               "duration": 0,
               "time_unit":
               {
                   "id": 0
               }
           },
           "release_notes": "",
           "status": 0,
           "is_active": true,
           "parent":
           {
               "id": 0
           },
           "release_type": 1,
           "children":
           [
               {
                   "id": 15,
                   "name": "V1",
                   "can_modify": true,
                   "start_date": "2013-10-31T19:00:00Z",
                   "due_date": null,
                   "velocity_start_date": null,
                   "capacity":
                   {
                       "duration": 0,
                       "time_unit":
                       {
                           "id": 0
                       }
                   },
                   "release_notes": "",
                   "status": 0,
                   "is_active": true,
                   "parent":
                   {
                       "id": 11
                   },
                   "release_type": 2,
                   "children":
                   [
                       {
                           "id": 16,
                           "name": "S1",
                           "can_modify": true,
                           "start_date": "2013-10-31T19:00:00Z",
                           "due_date": null,
                           "velocity_start_date": null,
                           "capacity":
                           {
                               "duration": 0,
                               "time_unit":
                               {
                                   "id": 0
                               }
                           },
                           "release_notes": "",
                           "status": 0,
                           "is_active": true,
                           "parent":
                           {
                               "id": 15
                           },
                           "release_type": 3,
                           "children":
                           [
                               {
                                   "id": 17,
                                   "name": "S1.1",
                                   "can_modify": true,
                                   "start_date": "2013-11-01T19:00:00Z",
                                   "due_date": null,
                                   "velocity_start_date": null,
                                   "capacity":
                                   {
                                       "duration": 0,
                                       "time_unit":
                                       {
                                           "id": 0
                                       }
                                   },
                                   "release_notes": "",
                                   "status": 0,
                                   "is_active": true,
                                   "parent":
                                   {
                                       "id": 16
                                   },
                                   "release_type": 3,
                                   "children":
                                   [
                                       {
                                           "id": 18,
                                           "name": "S.1.1.1",
                                           "can_modify": true,
                                           "start_date": "2013-11-02T00:00:00Z",
                                           "due_date": null,
                                           "velocity_start_date": null,
                                           "capacity":
                                           {
                                               "duration": 0,
                                               "time_unit":
                                               {
                                                   "id": 0
                                               }
                                           },
                                           "release_notes": "",
                                           "status": 11,
                                           "is_active": true,
                                           "parent":
                                           {
                                               "id": 17
                                           },
                                           "release_type": 3,
                                           "children":
                                           [
                                               {
                                                   "id": 20,
                                                   "name": "S1.1.1.1",
                                                   "can_modify": true,
                                                   "start_date": null,
                                                   "due_date": null,
                                                   "velocity_start_date": null,
                                                   "capacity":
                                                   {
                                                       "duration": 0,
                                                       "time_unit":
                                                       {
                                                           "id": 0
                                                       }
                                                   },
                                                   "release_notes": "",
                                                   "status": 0,
                                                   "is_active": true,
                                                   "parent":
                                                   {
                                                       "id": 18
                                                   },
                                                   "release_type": 3,
                                                   "children": null
                                               }
                                           ]
                                       }
                                   ]
                               }
                           ]
                       }
                   ]
               }
           ]
       }
   ]
}

我的结构就像发布 - >儿童 - >儿童 - >所以在子孩子的水平上是N ..但是我需要它们直到2级释放 - >孩子们就是这样。 我希望他们使用Gson映射到我的java类我的Java类结构将是这样的

Class release{
String id ;
String name ;
String start_date;
List<Children> children ;

}

Class Children{
String id ;
String name ;
String start_date;

}

1 个答案:

答案 0 :(得分:0)

你可以像这样达到n级或2级,这里是适用于你json格式的Java类

public class BaseModel {

    private String id;
    private String name;
    private String start_date;
    private boolean can_modify;
    private String due_date;
    private String velocity_start_date;
    private String Stringrelease_notes;
    private String status;
    private boolean is_active;
    private String release_type;
    // here getter/setter
}

发布课程

public class Release extends BaseModel {

    private List<Children> children;
    private Capacity capacity;
    private Parent parent;

    public List<Children> getChildren() { return children; }
    public void setChildren(List<Children> children) { this.children = children;}
    public Capacity getCapacity() { return capacity;}
    public void setCapacity(Capacity capacity) { this.capacity = capacity;}

    @Override
    public String toString() {
        return "Release [capacity=" + capacity + ", parent=" + parent
                + ", getChildren()=" + getChildren() + ", getCapacity()="
                + getCapacity() + "]";
    }

}

儿童班

public class Children extends BaseModel {

    private List<Children> children;
    private Capacity capacity;
    private Parent parent;

    public List<Children> getChildren() {return children;}
    public void setChildren(List<Children> children) {this.children = children;}

    @Override
    public String toString() {
        return "Children [capacity=" + capacity + ", parent=" + parent
                + ", getChildren()=" + getChildren() + "]";
    }
}

父类

public class Parent {

    private int id;
    @Override
    public String toString() { return "Parent [id=" + id + "]"; }
}

容量等级

public class Capacity {

    private int duration;
    private TimeUnit time_unit;

    @Override
    public String toString() {
        return "ChildCapacity [duration=" + duration + ", time_unit="+ time_unit + "]";
    }
}

只需致电

即可
Gson gson = new Gson();
 Release release =gson.fromJson(Your_Json_String, Release.class);
release.getCapacity();
release.getParent();
release.getChildren(); // return main array below
// go inside for nested level lik as
release.getChildren().get(0).getChildren(); // return child array.
release.getChildren().get(0).getChildren().get(0).getChildren();// return nested child array of above level

如果你想要检索父母的特定孩子,那么你需要在给json脱盐后实现你自己的递归。

享受! :)