1

I am trying to parse the following json into Java objects. It has an outer object called BaseCategory. BaseCategory has a Set of Category objects. A Category has a set of SubCategory Objects.

{
   "name":"base",
   "categories":[
        {
            "name":"category1",
            "subCategories": [
                   {
                       "name":"subCategory1"
                   },
                   {
                        "name": "subCategory2"
                   }
             ]
        },
        {
              "name":"category2",
              "subCategories": [
                    {
                          "name":"subCategory3"
                    },
                    {
                          "name": "subCategory4"
                    }
              ]

        }
   ]
}

The POJO classes are below. Stripped down to show just the relevant info. Could you please provide some help to parse this.

public class BaseCategory {
    private String name;
    private Set<Category> categories = new HashSet<Category>();
    //setters and getters
}

public class Category {
    private String name;
    private Set<SubCategory> subCategories = new HashSet<SubCategory>();
    //setters and getters
}

public class SubCategory {
     private String name;
     //setters and getters
}

1 Answers1

3

Use the following website, it will create your POJO's and you can use it directly. Link

Oguz Ozcan
  • 1,497
  • 13
  • 21
  • Thank you. I am trying to use the ObjectMapper to get the BaseCategory class with all the members properly populated. The link above provides the POJOs with annotations. Is there a way to get away without the annotations. – Ambuli Vaanan Nov 18 '17 at 06:31
  • Why don't you want to use annotations? – Oguz Ozcan Nov 18 '17 at 06:37
  • I am trying out your link you provided. So, if I replace all my pojo classes with the new ones, Can I just call BaseCategory base = new mapper.readValue(jsonFile, BaseCategory.class) to parse the json file? – Ambuli Vaanan Nov 18 '17 at 06:42
  • Yes :) I've just used these in these week and it saves lot's of time and effort. It will ask you package and class name at the right. Just type a class name here, I assume you want `BaseCategory` as the name, then you can use it as you said. – Oguz Ozcan Nov 18 '17 at 06:44