keshet
keshet

Reputation: 1826

Create JSON object in Java

I have to build in Java the following JSON object to send it to the client as a response to Ajax call:

{
    "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"
        }
        ]
    }
    ]
}

The big problem I have here is that the pairs: "name": "firm1", "name": "firm2", "name": "firm3", "name": "firm4" are sent to the client as object, so this object is considered as a key the the "projects":[...] part: enter image description here

Here is my Java code (for each client I try to connect its projects):

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);

How do I build the above JSON object? What do I miss in my code?

Thank you.

EDIT:

Instead of this block:

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);

I used this:

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

Now I get this object:

enter image description here

and I this is still isn't working. When I set the object that is in the first post as data source - everything works fine, but switching the data source to Ajax call response messes things up.

Further help is needed.

Thank you.

Upvotes: 0

Views: 184

Answers (1)

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

As per the JSON String you have posted name and projects should be attributes to the same object.

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

Explanation:

    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.

Upvotes: 1

Related Questions