Java 将JSon字符串解析到第n级

Java 将JSon字符串解析到第n级,java,json,gson,Java,Json,Gson,你好,我有一个Json字符串,它的深度在我的Json字符串之后达到了N级 { "id": 11, "name": "Release 5", "can_modify": true, "start_date": null, "due_date": null, "velocity_start_date": null, "capacity":

你好,我有一个Json字符串,它的深度在我的Json字符串之后达到了N级

{
           "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
                                               }
                                           ]
                                       }
                                   ]
                               }
                           ]
                       }
                   ]
               }
           ]
       }
   ]
}
我的结构类似于Release->Childern->Childern->等等,子级的子级是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;

}
类发布{
字符串id;
字符串名;
字符串开始日期;
列出儿童名单;
}
班级儿童{
字符串id;
字符串名;
字符串开始日期;
}

您可以像这样实现第n级或第2级,以下是适用于for You 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 + "]";
    }
}
容量等级

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后自行实现递归


享受!:)

那么您将如何处理级别大于2的子元素?丢弃它们?因为所有的子类都有子类,但是java类children.java没有类型也是children的成员,这真是一个矛盾。我想知道您的java表示是否不正确。JSON当前有一系列
Release
对象,每个对象都有一个子对象(这是一个Release对象),该JSON中没有
Children
对象。话虽如此,看起来您正试图在JSON中表示一个链表,因此我建议使用一个自定义解析器,或者将所有内容读取到一系列发布对象中,然后读取这些内容并将所有对象添加到原始对象的列表中。