在Java中创建JSON对象

时间:2014-09-18 06:20:29

标签: java json

我必须在Java中构建以下JSON对象,以将其作为对Ajax调用的响应发送到客户端:

{
    "firms": [
    {
        "name": "firm1",
        "projects": [
        {
            "name": "firm1project1"
        },
        {
            "name": "firm1project2"
        },
        {
            "name": "firm1project3"
        }
        ]
    },
    {
        "name": "firm2",
        "projects": [
        {
            "name": "firm2project1"
        },
        {
            "name": "firm2project2"
        },
        {
            "name": "firm2project3"
        }
        ]
    },
    {
        "name": "firm3",
        "projects": [
        {
            "name": "firm3project1"
        },
        {
            "name": "firm3project2"
        },
        {
            "name": "firm3project3"
        }
        ]
    },
    {
        "name": "firm4",
        "projects": [
        {
            "name": "firm4project1"
        },
        {
            "name": "firm4project2"
        },
        {
            "name": "firm4project3"
        }
        ]
    }
    ]
}

我在这里遇到的一个大问题是:"name": "firm1""name": "firm2""name": "firm3""name": "firm4"对作为对象发送到客户端,因此考虑此对象作为"projects":[...]部分的关键: enter image description here

这是我的Java代码(我尝试连接其项目的每个客户端):

while(i<aClients.size()){ //aClients - an array of clients

                query="select"+ 
                        " PROJECT_NAME"+
                        " from PROJECTS"+ 
                        " inner join CLIENTS"+ 
                        " on CLIENTS.CLIENT_ID=PROJECTS.CLIENT_ID"+
                        " where CLIENTS.CLIENT_ID="+"'"+aClients.get(i)+"'";
                result = statement.executeQuery(query);

        ArrayList<JSONObject> aProjects = new ArrayList<JSONObject>();

        while(result.next()){
                JSONObject oJsonInner = new JSONObject();
                oJsonInner.put("name",result.getString("project_name"));
                aProjects.add(oJsonInner);
        }

        //this is a problematic part -------------
        JSONObject oJsonClient = new JSONObject();
        oJsonClient.put("name", aClients.get(i));
        //end of problematic part ----------------

        JSONObject oJsonProjects = new JSONObject();
        oJsonProjects.put("projects", aProjects.toArray());

        JSONObject oJsonOuter = new JSONObject();
        oJsonOuter.put(oJsonClient.toString(),oJsonProjects);

        aJSONData.add(oJsonOuter);
        i++; // to cycle through clients array

}

jsonOutputObject.put("clients", aJSONData);

PrintWriter out = response.getWriter();
String json = new Gson().toJson(jsonOutputObject);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.write(json);

如何构建上述JSON对象?我的代码中有什么错过?

谢谢。

编辑:

而不是这个块:

JSONObject oJsonClient = new JSONObject();
oJsonClient.put("name", aClients.get(i));

JSONObject oJsonProjects = new JSONObject();
oJsonProjects.put("projects", aProjects.toArray());

JSONObject oJsonOuter = new JSONObject();
oJsonOuter.put(oJsonClient.toString(),oJsonProjects);

我用过这个:

JSONObject oJsonOuter = new JSONObject();
oJsonOuter.put("name", aClients.get(i));
oJsonOuter.put("projects", aProjects.toArray());

现在我得到了这个对象:

enter image description here

而我仍然无法正常工作。当我将第一篇文章中的对象设置为数据源时 - 一切正常,但将数据源切换到Ajax调用响应会让事情变得混乱。

需要进一步的帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

根据您发布的nameprojects的JSON字符串应该是同一对象的属性。

    JSONObject oJsonOuter = new JSONObject();
    oJsonOuter.put("name", aClients.get(i));
    oJsonOuter.put("projects", aProjects.toArray());

说明:

    JSONObject oJsonClient = new JSONObject();  -- First Object
    oJsonClient.put("name", aClients.get(i));   


    JSONObject oJsonProjects = new JSONObject();  -- Second Object
    oJsonProjects.put("projects", aProjects.toArray());

    JSONObject oJsonOuter = new JSONObject();
    oJsonOuter.put(oJsonClient.toString(),oJsonProjects);  -- You are making first object as key and second object as value here.