3

I'm trying to build a JSON object in my servlet. The object should look like this:

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

I have a problem in creating array of project names objects:

[
     {
         "name": "firm2project1"
     },
     {
         "name": "firm2project2"
     },
     {
         "name": "firm2project3"
     }
]

Right now I have the code as showed below (oJsonInner is a JSONObject object, aProjects - ArrayList of JSONObject type). I build the oJsonInner object from the results I get from database query:

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

Is there any way to get the value of the oJsonInner object in aProjects.add(oJsonInner); so during the next loop I could create a new oJsonInner object with different "project_name" value without updating the object that got into aProjects array during the first loop?

Cœur
  • 37,241
  • 25
  • 195
  • 267
keshet
  • 1,716
  • 5
  • 27
  • 54

5 Answers5

5
while(result.next()){
   oJsonInner = new JsonObject();
   oJsonInner.put("name",result.getString("project_name"));
   aProjects.add(oJsonInner);
}
Mark
  • 2,167
  • 4
  • 32
  • 64
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
1

Try this method:

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

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

RESULT:

[{"name":"project1"}, {"name":"project2"}]
Srinu Chinka
  • 1,471
  • 13
  • 19
Kumar Kailash
  • 1,385
  • 1
  • 10
  • 19
1

you can use a JSONArray object and add it JSON object.

while(result.next()){
  JSONObject oJsonInner = new JSONObject();
  JSONArray arr = new JSONArray();
  json.put("name",result.getString("project_name"));
  arr.put(json);
}
Parth Srivastav
  • 316
  • 2
  • 13
0

Use Gson lib that help you to serialize json format which take java bean and convert it to json format.

Model

public class Firm {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private List<Project> projetcts;


public List<Project> getProjetcts() {
    return projetcts;
}

public void setProjetcts(List<Project> projetcts) {
    this.projetcts = projetcts;
}


public static class Project{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
}

Gson code

 public static void main(String[] args) {
    Firm [] firms = new Firm[2]; 

    Project p1 = new Project();
    p1.setName("project 1");
    Project p2 = new Project();
    p2.setName("project 2");
    Project p3 = new Project();
    p3.setName("project 3");
    List<Project> projects = new ArrayList<Firm.Project>();
    projects.add(p1);
    projects.add(p2);
    projects.add(p3);

    Firm firm1 = new Firm();
    firm1.setName("firm1");
    firm1.setProjetcts(projects);

    Firm firm2 = new Firm();
    firm2.setName("firm2");
    firm2.setProjetcts(projects);

    firms[0] = firm1;
    firms[1] = firm2;

    String jsonText = new Gson().toJson(firms);
    System.out.println(jsonText);

}

Result Sample

[{"name":"firm1","projetcts":[{"name":"project 1"},{"name":"project 2"},{"name":"project 3"}]},{"name":"firm2","projetcts":[{"name":"project 1"},{"name":"project 2"},{"name":"project 3"}]}]

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
0

Check this solution, it may satisfy your expectations:

UPDATE:

In your case, the needed JSON object can be created like so (note that actual key-value lines can be imported from external files or other sources like socked, db and so on):

System.out.println(
        JsonBuilder.of()
                .add("[0].name", "firm2project1")
                .add("[1].name = firm2project2")
                .add("[2].name: \"firm2project3\"")
                .build()
                .toPrettyString()
);

, which results to:

[
  {
    "name": "firm2project1"
  },
  {
    "name": "firm2project2"
  },
  {
    "name": "firm2project3"
  }
]
Teodor
  • 114
  • 7