5

I am receiving a following list from DB which I want to convert to JSON:

Employee e1=new Employee(101,"Ha","De","Acr");
Employee e2=new Employee(102,"D ","Forouzan","Mc");
Employee e3=new Employee(102,"Op","Ga","Wi");
Employee e4=new Employee(101,"YUI","HI","EX");

I want to change it just it tranverses the above received list and for duplicate keys (101, 102) it creates one jsoobject using array;

ex: 101 : {["Ha","De","Acr"],["YUI","HI","EX"]}
harsh
  • 51
  • 1
  • 5

3 Answers3

4

I tried the below one and got the expected result :

public static void main(String[] args) {

    Employee e1 = new Employee(101, "Ha", "De", "Acr");
    Employee e2 = new Employee(102, "D ", "Forouzan", "Mc");
    Employee e3 = new Employee(102, "Op", "Ga", "Wi");
    Employee e4 = new Employee(101, "YUI", "HI", "EX");

    List<Employee> employeeList1 = new ArrayList<>();
    employeeList1.add(e1);      
    employeeList1.add(e4);

    List<Employee> employeeList2 = new ArrayList<>();
    employeeList2.add(e2);
    employeeList2.add(e3);

    Map<Integer, ArrayList<Employee>> map = new HashMap<Integer, ArrayList<Employee>>();
    // As of now , I have populated the map directly, but you can place some logic here for putting the values here dynamically.
    map.put(101, (ArrayList<Employee>) employeeList1);
    map.put(102, (ArrayList<Employee>) employeeList2);

    ObjectMapper mapper = new ObjectMapper();
    try {
        String json = mapper.writeValueAsString(map);
        System.out.println(json);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Punit
  • 324
  • 1
  • 4
  • 17
  • Try it and let me know for any confusion. – Punit Sep 14 '17 at 09:08
  • Thanks but I have eited the question which i am facing. – harsh Sep 14 '17 at 09:35
  • Still , I'm not sure about your problem statement. Will you please elaborate ? – Punit Sep 14 '17 at 11:27
  • I am getting the following object in Object [] obj after executing query; (101, "Ha", "De", "Acr"); (102, "D ", "Forouzan", "Mc"); (102, "Op", "Ga", "Wi"); (101, "YUI", "HI", "EX"); I want to tranverse the above received list and also for duplicate keys (101, 102) it creates one jsoobject using array. which is also the output of yours in above program you posted. Main point is that here there are only 4 list objects i take as example, query may also return 10 or 50 rows. – harsh Sep 14 '17 at 12:12
  • For this, you first need to iterate the object array Object[] and then need to populate the Map> in such a way that it will have all the duplicate objects under the list. Therefore, already I put an inline comment in my answer over there. – Punit Sep 14 '17 at 12:26
  • Use below at the place of comment : for (int i = 0; i < obj.length; i++) { Employee e = (Employee) obj[i]; if (map.containsKey(e.getEmpId())) { map.get(e.getEmpId()).add(e); } else { map.put(e.getEmpId(), Arrays.asList(e)); } } – Punit Sep 14 '17 at 12:46
  • I'm wondering if this answer's upvoters really understand the question and if they executed the code above which prints `{"102":[{"id":102,"name1":"D ","name2":"Forouzan","name3":"Mc"}` and so on which clearly differs from the OP requirements. – fantaghirocco Sep 14 '17 at 12:48
0

I'll try an answer.

List<Employee> list = //Our employees
Map x = new HashMap();
for(int y = 0; y < list; y++){
  x.put(list[y].value0, new Array(list[y].value1, list[y].value2, list[y].value3));
}
JSONObject json = new JSONObject();
json.putAll( x );

And that should do what you needed! Try it and let me know.

S.V.
  • 1,181
  • 6
  • 23
0

It is a bit unclear what you're asking because your example output isn't valid JSON. I am assuming here that you'd want to map employee identifiers to lists of employees, which you can model with a Map<Integer, List<Employee>>. To produce JSON from this data structure, you need an external library, such as Jackson JSON library.

Assuming the following Employee class

public class Employee {

    private final int id;
    private final String name1;       
    private final String name2;
    private final String name3;

    public Employee(int id, String name1, String name2, String name3) {
        this.id = id;
        this.name1 = name1;
        this.name2 = name2;
        this.name3 = name3;
    }

    public int getId() {
        return id;
    }
    public String getName1() {
        return name1;
    }
    public String getName2() {
        return name2;
    }
    public String getName3() {
        return name3;
    }        
}

And marshaling code

Employee e1 = new Employee(101,"Ha","De","Acr");
Employee e2 = new Employee(102,"D ","Forouzan","Mc");
Employee e3 = new Employee(102,"Op","Ga","Wi");
Employee e4 = new Employee(101,"YUI","HI","EX");

Map<Integer, List<Employee>> employees = new HashMap<>();
employees.put(101, Arrays.asList(e1, e4));
employees.put(102, Arrays.asList(e2, e3));

String json = new ObjectMapper().writerWithDefaultPrettyPrinter()
    .writeValueAsString(employees);        
System.out.println(json);

you'll get this JSON:

{
  "102" : [ {
    "id" : 102,
    "name1" : "D ",
    "name2" : "Forouzan",
    "name3" : "Mc"
  }, {
    "id" : 102,
    "name1" : "Op",
    "name2" : "Ga",
    "name3" : "Wi"
  } ],
  "101" : [ {
    "id" : 101,
    "name1" : "Ha",
    "name2" : "De",
    "name3" : "Acr"
  }, {
    "id" : 101,
    "name1" : "YUI",
    "name2" : "HI",
    "name3" : "EX"
  } ]
}

Required Maven dependencies for the code:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.0</version>
</dependency>
<!-- Jackson databinding; ObjectMapper, JsonNode and related classes are here -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.0</version>
</dependency>
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30