1

I have a very simple question which doesnt seem to have an answer.

I have an object which has 2 objects with same type inside it and they are not arrays. How do i represent it in json?

GroupOfEmployees{

Employee emp1;
Employee emp2;
}

Employee{
String name;
int id;
}

emp1 details: name:first id: 1

emp2 details: name: second id:2

2 Answers 2

1

Make a JSONArray:

[
{
name: "name1",
id: = id1 }, 
{
name: "name2",
id: = id2 }
]
Sign up to request clarification or add additional context in comments.

Comments

1

if you are looking for the json object which represents GroupOfEmployees object then the sample json will be something like below

{
    "emp1": {
        "name": "name1",
        "id": 1
    },
    "emp2": {
        "name": "name2",
        "id": 2
    }
}

update
when you convert above json into object, you will get an object which holds two employee objects with name emp1 & emp2. Consider the following example

The following is your Employee Class:

public class Employee {
    private String name;

    private int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }

    // getter & setter methods

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Employee [name=");
        builder.append(name);
        builder.append(", id=");
        builder.append(id);
        builder.append("]");
        return builder.toString();
    }
}

The following is GroupOfEmployees class:

public class GroupOfEmployees {
    private Employee emp1;
    private Employee emp2;

    // getters & setters method

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("GroupOfEmployees [emp1=");
        builder.append(emp1);
        builder.append(", emp2=");
        builder.append(emp2);
        builder.append("]");
        return builder.toString();
    }
}

here goes main method:

public static void main(String[] args) {
    Employee emp1 = new Employee("name1", 1);
    Employee emp2 = new Employee("name2", 2);

    GroupOfEmployees groupOfEmployees = new GroupOfEmployees();
    groupOfEmployees.setEmp1(emp1);
    groupOfEmployees.setEmp2(emp2);

    Gson gson = new GsonBuilder().create();

    String jsonValue = gson.toJson(groupOfEmployees);
    System.out.println("jsonValue = " + jsonValue);

    GroupOfEmployees newGroup = gson.fromJson(jsonValue, GroupOfEmployees.class);
    System.out.println("Object value = " + newGroup);
}

and finally output is :

jsonValue = {"emp1":{"name":"name1","id":1},"emp2":{"name":"name2","id":2}}
Object value = GroupOfEmployees [emp1=Employee [name=name1, id=1], emp2=Employee [name=name2, id=2]]

2 Comments

Actually I want to make a json which can be reconverted to the mentioned objects later using gson library. So in this solution it will convert this json to an array of employee objects with 2 elements, which I want to avoid. I want them to be converted to 2 separate objects exactly as shown in the question . How to achieve that?
+1 coz u understood the question correctly.Can you explain how is the toString method getting called?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.