我无法理解如何在一个哈希映射中导入一个或多个与父亲一起的每个孩子的"名称" ,以及如何创建一个能让我回归的方法她孩子的父亲。 任何人都可以帮助我吗?
JSON
{
"child": [
{
"name": "one",
"child": [
{
"id": "0001",
"name": "oneone",
"image": "one.jpg",
"child": [
{
"id": "1",
"name": "oneoneone",
"child": [
{
"id": "1",
"name": "oneoneoneone",
"child": [
{
"id": "1",
"name": "oneoneoneoneone"
},
{
"id": "2",
"name": "oneoneoneonetwo"
},
{
"id": "3",
"name": "oneoneoneonethree"
}
]
},
{
"id": "2",
"name": "oneoneonetwo"
},
{
"id": "3",
"name": "oneoneonethree"
}
]
},
{
"id": "2",
"name": "oneonetwo"
}
]
},
{
"id": "0002",
"name": "onetwo",
"image": "two.jpg"
},
{
"id": "onethree",
"name": "three",
"image": "three.jpg"
}
]
},
{
"name": "two",
"child": [
{
"id": "0004",
"name": "twoone",
"image": "one.jpg"
},
{
"id": "0005",
"name": "twotwo",
"image": "two.jpg",
"child": [
{
"id": "1",
"name": "twotwoone",
"child": [
{
"id": "1",
"name": "twotwooneone"
},
{
"id": "2",
"name": "twotwoonetwo"
},
{
"id": "3",
"name": "twotwoonethree"
}
]
},
{
"id": "2",
"name": "twotwotwo"
}
]
},
{
"id": "0006",
"name": "twothree",
"image": "three.jpg"
}
]
}
]
}
子
public class Child {
private String id;
private String name;
private String image;
private List<Child> child = new ArrayList<Child>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image= image;
}
public List<Child> getChild() {
return child;
}
public void setChild(List<Child> child) {
this.child = child;
}
}
答案 0 :(得分:0)
试试这段代码。要获取给定子项的父项,请使用getParentFor
方法,其中第一个arg是结构的根,第二个是给定子项的名称。
public class GsonExample1 {
interface ExampleVisitor {
void onParentStart(Example example);
void onChild(Example example);
}
static class Example {
private String id;
private String name;
private String immagine;
private Example[] child;
public void accept(ExampleVisitor visitor) {
visitor.onParentStart(this);
if (child != null) {
for (Example c : child) {
visitor.onChild(c);
}
for (Example c : child) {
c.accept(visitor);
}
}
}
@Override
public String toString() {
return "Example [id=" + id + ", name=" + name + ", immagine="
+ immagine + ", child=" + Arrays.toString(child) + "]";
}
}
public static Example getParentFor(Example root, final String name) {
final Example[] bag = new Example[1];
root.accept(new ExampleVisitor() {
private Example currentParent;
public void onParentStart(Example example) {
currentParent = example;
}
public void onChild(Example example) {
if (name.equals(example.name)) {
bag[0] = currentParent;
}
}
});
return bag[0];
}
public static Example[] getAllChildrenFor(Example root, final String name) {
final Example[] bag = new Example[1];
root.accept(new ExampleVisitor() {
public void onParentStart(Example example) {
if (name.equals(example.name)) {
bag[0] = example;
}
}
public void onChild(Example example) {
}
});
return bag[0] != null ? bag[0].child : null;
}
public static void main(String[] args) {
GsonExample1.class.getResourceAsStream("sample1.txt");
Gson gson = new Gson();
Example root = gson.fromJson(
new InputStreamReader(GsonExample1.class
.getResourceAsStream("sample1.txt")), Example.class);
System.out.println(root);
System.out.println(getParentFor(root, "oneoneone"));
System.out.println(Arrays
.toString(getAllChildrenFor(root, "oneoneone")));
}
}
输出
Example [id=null, name=null, immagine=null, child=[Example [id=null, name=one, immagine=null, child=[Example [id=0001, name=oneone, immagine=one.jpg, child=[Example [id=1, name=oneoneone, immagine=null, child=[Example [id=1, name=oneoneoneone, immagine=null, child=[Example [id=1, name=oneoneoneoneone, immagine=null, child=null], Example [id=2, name=oneoneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneonetwo, immagine=null, child=null]]], Example [id=0002, name=onetwo, immagine=two.jpg, child=null], Example [id=onethree, name=three, immagine=three.jpg, child=null]]], Example [id=null, name=two, immagine=null, child=[Example [id=0004, name=twoone, immagine=one.jpg, child=null], Example [id=0005, name=twotwo, immagine=two.jpg, child=[Example [id=1, name=twotwoone, immagine=null, child=[Example [id=1, name=twotwooneone, immagine=null, child=null], Example [id=2, name=twotwoonetwo, immagine=null, child=null], Example [id=3, name=twotwoonethree, immagine=null, child=null]]], Example [id=2, name=twotwotwo, immagine=null, child=null]]], Example [id=0006, name=twothree, immagine=three.jpg, child=null]]]]]
小孩oneoneone
的父级是
Example [id=0001, name=oneone, immagine=one.jpg, child=[Example [id=1, name=oneoneone, immagine=null, child=[Example [id=1, name=oneoneoneone, immagine=null, child=[Example [id=1, name=oneoneoneoneone, immagine=null, child=null], Example [id=2, name=oneoneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneonetwo, immagine=null, child=null]]]