0

I would like to be able to parse and print out some information from a JSON file however I get stuck whenever I try to do it with nested objects-arrays.

I am using JsonSimple library.

My json file:

x

What I have been doing so far was to modify the file into the following:

x

And by using an iterator was able to print out all the info:

x

As soon as I modify the JSON file and add the Staff object it stops working. I know I am doing something wrong but I can't see what.

I would really like to be able to print out this info with the Staff object included in the JSON file and once that's done say I need to print out these 2 managers in ascending age order how will I do that?

All ideas and suggestions will be so much appreciated ! Thanks.

The Manager class:

x
  • With the outer Staff object, you would first have to get "Staff" out of your object, as a JSONObject, and then use the code you already have to get the "Managers" array out of this JSONObject. – JB Nizet Mar 14 '15 at 14:36
  • So something like declaring all types within the array? JsonObject staff = jsonObject.get( "Staff" ).asObject(); and then use my code? – LogitechJuice Mar 14 '15 at 14:44
  • Getting even more confused but will try looking into that suggestion. Thank you for your time. – LogitechJuice Mar 14 '15 at 14:50

1 Answers1

1

first you need to get Staff object from that you need to get Manager

List<Manager> managersList = new ArrayList<Manager>();

JSONObject staff = jsonObject.getJSONObject("Staff");
JSONArray managers = staff.getJSONArray("Managers");

for (int index = 0; index < managers.length(); index++) {
    JSONObject resultObject = managers.getJSONObject(index);
    System.out.println(resultObject);

    Manager manager = new Manager();
    manager.setName(resultObject.getString("name");
    manager.setAge(resultObject.getInt("age");
    manager.setPosition(resultObject.getString("position");

    managersList.add(manager);
}

once you construct Manager array list you can use the following to sort your array list.

Collections.sort(managersList, new Comparator<Manager>() {
    @Override
    public int compare(Manager object1, Manager object2) {
        return Integer.compare(object1.getAge(), object2.getAge());
    }
});

Update:

Here goes your complete working solution:

Manager:

public class Manager {
    private int age;
    private String name;
    private String position;

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

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

    public String getPosition() {
        return this.position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}

Main method:

public static void main(String[] args) {
    JSONObject jsonObject = new JSONObject("{\"Staff\": {\"Managers\": [{\"name\": \"Chris\",\"age\": 43,\"position\": \"Retail\"},{\"name\": \"John\",\"age\": 36,\"position\": \"Sales\"}]}}");

    List<Manager> managersList = new ArrayList<Manager>();

    JSONObject staff = jsonObject.getJSONObject("Staff");
    JSONArray managers = staff.getJSONArray("Managers");

    for (int index = 0; index < managers.length(); index++) {
        JSONObject resultObject = managers.getJSONObject(index);

        Manager manager = new Manager();
        manager.setName(resultObject.getString("name"));
        manager.setAge(resultObject.getInt("age"));
        manager.setPosition(resultObject.getString("position"));

        managersList.add(manager);
    }

    Collections.sort(managersList, new Comparator<Manager>() {
        @Override
        public int compare(Manager object1, Manager object2) {
            return Integer.compare(object1.getAge(), object2.getAge());
        }
    });

    for (Manager iterator : managersList) {
        System.out.println(iterator.getName() + " --- " + iterator.getAge() + " --- " + iterator.getPosition());
    }
}

output:

John --- 36 --- Sales
Chris --- 43 --- Retail
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59