I am trying to build my object using the below Json payload(which is being fetched from external API), now when ever i use any get method(i.e: jsonObject.getString() or jsonObject.getBoolean(), jsonObject.getObject()) and if the value present for the key is null,I am getting an exception that "the value is not a string/boolean/Jsonobject" now how can i handle this, since if the value is present in object 1 on the above array index 0, it may/maynot present in another object at index 1

here I can apply null checks to resolve this issue, but this object has more than 40 key value pairs and add these many null checks may become cumbersome, is there any better way to resolve this?

{
  "data":[
    {
      "employer_name":"Dice",
      "employer_logo":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKlgydP7sElaJC9qPrtNHwBhyTMHYgii1RPWsy&s=0",
      "employer_website":null,
      "employer_company_type":"Information",
      "job_publisher":"LinkedIn",
      "job_id":"8yv3oA_2-UYAAAAAAAAAAA==",
      "job_employment_type":"CONTRACTOR",
      "job_title":"Web Developer - 6-month Contract - Houston Hybrid",
      "job_apply_link":"https://www.linkedin.com/jobs/view/web-developer-6-month-contract-houston-hybrid-at-dice-3624857671",
      "job_apply_is_direct":false
    },
    {
      "employer_name":"Dicenvskjvks",
      "employer_logo":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKlgydP7sElaJC9qPrtNHwBhyTMHYgii1RPWsy&s=0",
      "employer_website":"https://www.someUrl.com",
      "employer_company_type":"Information",
      "job_publisher":"LinkedIn",
      "job_id":"8yv3oA_2-UYAAAAAAAAAAA==",
      "job_employment_type":null,
      "job_title":"Web Developer - 6-month Contract - Houston Hybrid",
      "job_apply_link":"https://www.linkedin.com/jobs/view/web-developer-6-month-contract-houston-hybrid-at-dice-3624857671",
      "job_apply_is_direct":false
    }
  ]
}

My code:


    public static List<Response> parseJServer1Response(String responseBody) throws ParseException, JsonProcessingException {
        JSONObject responseObject = new JSONObject(responseBody);
        JSONArray data = responseObject.getJSONArray("data");
        List<Response> response = new ArrayList<>();

        for (int i = 0; i < data.length(); i++) {
            JSONObject jsonObject = data.getJSONObject(i);
            JSearch job = Response.builder()
                    .companyName(jsonObject.getString("employer_name"))
                    .companyLogo(jsonObject.getString("employer_logo"))
                    .companyType(jsonObject.getString("employer_company_type"))
                    .jobPublisher(jsonObject.getString("job_publisher"))
            ....
            ....
            ....
            ....
            ....
            ....
            .build();
            response.add(job);
        }
        return response;
    }
1

There are 1 best solutions below

0
Daniel Hernández On BEST ANSWER

Use optString, optBoolean and so on:

        for (int i = 0; i < data.length(); i++) {
            JSONObject jsonObject = data.getJSONObject(i);
            jsonObject.optString("employer_name", "default_value");
        }

check documentation of JSONObject.

For your other question, there are libraries like Gson for turning a jsonString to a class, check this guide and this other SO question