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)\"" +
" }]" +
"}]";
上記のコードを実行すると、レベルの子にのみ表示されます。しかし、私はオブジェクトのリスト全体を繰り返したいと思っています。
何か案が?助けてください。