1

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

上記のコードを実行すると、レベルの子にのみ表示されます。しかし、私はオブジェクトのリスト全体を繰り返したいと思っています。

何か案が?助けてください。

4

1 に答える 1

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 文字列をフォーマットするには、Notepad++ またはその他のツールを使用します。このようにして、いくつかのエラーを回避できます

于 2013-09-21T08:02:45.633 に答える