2013-09-21 50 views
1

我從JSON字符串生成JAVA對象。但是我在迭代列表項時遇到了問題。這個JSON是一個嵌套數組。這是我的Java代碼如何迭代由GSON製作的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)\"" + 
      " }]" + 
      "}]"; 

當我運行上面的代碼只顯示在關卡的兒童中。但是我想迭代整個對象列表。

有什麼想法?請幫忙。

回答

0

使用遞歸超過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字符串。通過這種方式,你可以避免一些錯誤

+0

這很好,但當我更改JSON字符串,就像我添加更多的孩子,並使其更加嵌套時,它再次只顯示我2個孩子。我的json是動態生成的,可以達到用戶想要的儘可能多的孩子。 – user2777070

+0

你可以發佈更多的孩子json嗎?從你的代碼'Employees emp = l.get(0);'我們只使用第一個孩子,所以... –

+0

我改變了我的代碼,現在試試,運行所有的孩子 –