2013-09-21 2 views
1

JSON String에서 JAVA Object를 생성하고 있습니다. 하지만 목록 항목을 반복 할 때 문제가 발생합니다. 이 JSON은 중첩 배열입니다. 여기에 내가 실행하면 내 JSON 문자열GSON에서 만든 JAVA Object LIST를 반복하는 방법

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

내 자바 코드

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/>"); 
      } 

입니다 내 자바 사용자 정의 클래스는

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 숨겨진 필드 이리저리오고있다 위 코드는 레벨 어린이에게만 표시됩니다. 하지만 개체의 전체 목록을 반복하고 싶습니다.

아이디어가 있으십니까? 도와주세요.

답변

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/> 

메모장 ++ 또는 기타 도구를 사용하여 Json 문자열의 서식을 지정하십시오. 이런 식으로 약간의 오류를 피할 수 있습니다

+0

이것은 괜찮습니다.하지만 json 문자열을 변경할 때 더 많은 자식을 추가하고 더 중첩 시키면 다시 2 개의 childerens 만 표시됩니다. 내 json은 동적으로 생성되며, 많은 사용자가 원하는만큼 많은 자식이 될 수 있습니다. – user2777070

+0

더 많은 자녀가있는 json을 게시 할 수 있습니까? 당신의 코드에서'Employees emp = l.get (0);'우리는 첫 번째 자식 만 사용하므로 ... –

+0

내 코드를 변경하고, 지금 시도하고, 모든 자식을 돌리십시오. –

관련 문제