簡體   English   中英

用Java創建JSON對象

[英]Create JSON object in Java

我必須在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": "firm1""name": "firm2""name": "firm3""name": "firm4"作為對象發送給客戶端,因此,該對象被視為"projects":[...]部分的關鍵: 在此處輸入圖片說明

這是我的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());

現在我得到了這個對象:

在此處輸入圖片說明

我仍然無法正常工作。 當我將第一篇文章中的對象設置為數據源時,一切正常,但是將數據源切換到Ajax調用響應會使事情變得混亂。

需要進一步的幫助。

謝謝。

根據JSON字符串,您已經發布了name並且projects應該是同一對象的屬性。

    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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM