java - 如何迭代由 GSON 生成的 JAVA 对象列表

标签 java json list gson

我正在从 JSON 字符串生成 JAVA 对象。但我在迭代列表项时遇到问题。此 JSON 是一个嵌套数组。这是我的java代码

 response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    nString xr = request.getParameter("JSONString");
    Gson gson = new Gson(); 
    java.lang.reflect.Type type = new TypeToken<List<EmployeeJSONObj>>(){}.getType();
    List<EmployeeJSONObj>l = gson.fromJson(xr, type);
    List<EmployeeJSONObj>l1 = l.get(0).getChild();

for(int i=0;i<l1.size();i++)
           {
               out.println("Name: "+l1.get(i).getName()+"<br/>");
           }

并且我的 java 自定义类是

public  class EmployeeJSONObj {
    private String name;
    private List<EmployeeJSONObj> children = new LinkedList<>();
    EmployeeJSONObj()
    {

    }
    public String getName()
    {
        return name;
    }

    public List<EmployeeJSONObj> getChild()
    {
        return children;
    }

    public String toString() {
        return "name: " + name + ", children = " + children;
    }

}

并且 JSON 字符串来自 HTML 隐藏字段,这是我的 json 字符串

String str = "[{" + 
            "   \"name\": \"3214657890RootSAPSSE\"," + 
            "   \"children\": [{" + 
            "       \"name\": \"672BENIAMEEN .Sales Base Market SectionCustomer Representative\"," + 
            "       \"children\": [{" + 
            "           \"name\": \"672BENIAMEEN .Sales Base Market SectionPeão\"," + 
            "           \"children\": [{" + 
            "               \"name\": \"910MAZHAR .Sales Base Market SectionCustomer Representative\"," + 
            "               \"children\": [{" + 
            "                   \"name\": \"910MAZHAR .Sales Base Market SectionPeão\"," + 
            "                   \"children\": [{" + 
            "                       \"name\": \"713NOSHERWAN .Sales Sargodha SectionCustomer Representative\"," + 
            "                       \"children\": [{" + 
            "                           \"name\": \"713NOSHERWAN .Sales Sargodha SectionPeão\"" + 
            "                       }," + 
            "                       {" + 
            "                           \"name\": \"1161SAQLAIN .Sales Toba Taik Singh SecPeão\"" + 
            "                       }]" + 
            "                   }]" + 
            "               }]" + 
            "           }]" + 
            "       }," + 
            "       {" + 
            "           \"name\": \"1161SAQLAIN .Sales Toba Taik Singh SecCustomer Representative\"," + 
            "           \"children\": [{" + 
            "               \"name\": \"1179SHAMOON .Administration SectionDriver ( R )\"" + 
            "           }]" + 
            "       }]" + 
            "   }," + 
            "   {" + 
            "       \"name\": \"1179SHAMOON .Farooq Khan TrustDriver ( D)\"" + 
            "   }]" + 
            "}]";

当我运行上面的代码时,它只显示在 child 级别。但我想迭代整个对象列表。

有什么想法吗?请帮忙。

最佳答案

使用递归来运行EmployeeJSONObj的子级

public class Help {

public static void main(String args[]) {
    String str = "[{" + 
            "   \"name\": \"3214657890RootSAPSSE\"," + 
            "   \"children\": [{" + 
            "       \"name\": \"672BENIAMEEN .Sales Base Market SectionCustomer Representative\"," + 
            "       \"children\": [{" + 
            "           \"name\": \"672BENIAMEEN .Sales Base Market SectionPeão\"," + 
            "           \"children\": [{" + 
            "               \"name\": \"910MAZHAR .Sales Base Market SectionCustomer Representative\"," + 
            "               \"children\": [{" + 
            "                   \"name\": \"910MAZHAR .Sales Base Market SectionPeão\"," + 
            "                   \"children\": [{" + 
            "                       \"name\": \"713NOSHERWAN .Sales Sargodha SectionCustomer Representative\"," + 
            "                       \"children\": [{" + 
            "                           \"name\": \"713NOSHERWAN .Sales Sargodha SectionPeão\"" + 
            "                       }," + 
            "                       {" + 
            "                           \"name\": \"1161SAQLAIN .Sales Toba Taik Singh SecPeão\"" + 
            "                       }]" + 
            "                   }]" + 
            "               }]" + 
            "           }]" + 
            "       }," + 
            "       {" + 
            "           \"name\": \"1161SAQLAIN .Sales Toba Taik Singh SecCustomer Representative\"," + 
            "           \"children\": [{" + 
            "               \"name\": \"1179SHAMOON .Administration SectionDriver ( R )\"" + 
            "           }]" + 
            "       }]" + 
            "   }," + 
            "   {" + 
            "       \"name\": \"1179SHAMOON .Farooq Khan TrustDriver ( D)\"" + 
            "   }]" + 
            "}]";

    System.out.println(str);

    Gson gson = new Gson(); 
    Type type = new TypeToken<List<Employees>>(){}.getType();
    List<Employees >l = gson.fromJson(str, type);
    System.out.println(l);

    iterateOverEmployee(l); 
}

private static void iterateOverEmployee(List<EmployeeJSONObj> l) {
    for(EmployeeJSONObj emp: l){

        if(emp.getChildren() != null){

            for(int i=0;i<emp.getChildren().size();i++)
            {
                System.out.println("Name: "+emp.getChildren().get(i).getName()+"<br/>");
            }

            iterateOverEmployee(emp.getChildren());
        }           
    }
}

EmployeeJSONObj

public static  class EmployeeJSONObj {
    private String name;
    private List<EmployeeJSONObj> children;


    public String getName()
    {
        return name;
    }

    public String toString() {
        return "name: " + name + ", children = " + children;
    }
    public List<EmployeeJSONObj> getChildren() {
        return children;
    }
    public void setChildren(List<EmployeeJSONObj> children) {
        this.children = children;
    }

}

输出:

Name: 672BENIAMEEN .Sales Base Market SectionCustomer Representative<br/>
Name: 1179SHAMOON .Farooq Khan TrustDriver ( D)<br/>
Name: 672BENIAMEEN .Sales Base Market SectionPeão<br/>
Name: 1161SAQLAIN .Sales Toba Taik Singh SecCustomer Representative<br/>
Name: 910MAZHAR .Sales Base Market SectionCustomer Representative<br/>
Name: 910MAZHAR .Sales Base Market SectionPeão<br/>
Name: 713NOSHERWAN .Sales Sargodha SectionCustomer Representative<br/>
Name: 713NOSHERWAN .Sales Sargodha SectionPeão<br/>
Name: 1161SAQLAIN .Sales Toba Taik Singh SecPeão<br/>
Name: 1179SHAMOON .Administration SectionDriver ( R )<br/>

作为旁注

使用 Notepad++ 或其他工具格式化 Json 字符串。通过这种方式可以避免一些错误

关于java - 如何迭代由 GSON 生成的 JAVA 对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18930184/

相关文章:

java - 我可以编写一个抛出异常的函数,该函数可以在 android 的 onCreate 中使用吗?

json - 复制 package.json - Dockerfile

javascript - Restangular:无法发布复杂的 json 参数?

Python,需要长整型值

java - Ant + 保留控制值

java - 如何判断用户喜欢简体字还是繁体字

java - 使用 itext pdf stamper 在 pdf 中添加多个附件

r - 查找配对之间最常见的组合

python - 如果后面有另一个值,则删除列表值

c# - 如何在 C# 中列出 .zip 文件夹的内容?