【问题标题】:How to Iterate the JAVA Object LIST made from GSON如何迭代由 GSON 生成的 JAVA 对象列表
【发布时间】:2013-09-26 15:12:26
【问题描述】:

我正在从 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;
    }

}

AND 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)\"" + 
            "   }]" + 
            "}]";

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

有什么想法吗?请帮忙。

【问题讨论】:

    标签: java json list gson


    【解决方案1】:

    使用递归遍历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 字符串。这样可以避免一些错误

    【讨论】:

    • 这很好,但是当我更改 json 字符串时,例如当我添加更多子项并使其更嵌套时,它再次显示我只有 2 个子项。我的 json 是动态生成的,并且可以生成任意多个用户想要的子节点。
    • 你能用更多的孩子发布 json 吗?从您的代码Employees emp = l.get(0); 我们只使用第一个孩子,所以...
    • 这是我从 HTML 隐藏字段中获取的 JSON 字符串。 [{"name":"3214657890RootSAPSSE","children":[{"name":"672BENIAMEEN .Sales Base Market Section客户代表","children":[{"name":"672BENIAMEEN .Sales Base Market SectionPeão"," children":[{"name":"910MAZHAR .Sales Base Market Section客户代表","children":[{"name":"910MAZHAR .Sales Base Market SectionPeão","children":
    • [{"name":"713NOSHERWAN .Sales Sargodha Section客户代表","children":[{"name":"713NOSHERWAN .Sales Sargodha SectionPeão"},{"name":"1161SAQLAIN .销售 Toba Taik Singh SecPeão"}]}]}]}]},{"name":"1161SAQLAIN .Sales Toba Taik Singh Sec客户代表","children":[{"name":"1179SHAMOON .Administration SectionDriver (R) "}]}]},{"name":"1179SHAMOON .Farooq Khan TrustDriver (D)"}]}]
    • 这是我从您的输出中得到的结果 [名称:3214657890RootSAPSSE,儿童 = [名称:672BENIAMEEN .Sales Base Market Section 客户代表,名称:1179SHAMOON .Farooq Khan TrustDriver (D)]]跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2018-02-19
    • 2021-08-27
    相关资源
    最近更新 更多