Check if a particular JSON Object is available or not

前端 未结 3 2166
太阳男子
太阳男子 2021-02-13 04:38

I have JSON File as below which has to be dumped into an ArrayList:

{
 \"main1\" : [
  {
     \"child1\" : valueA,
     \"child2\" : valueB,
     \"child3\" : va         


        
3条回答
  •  遇见更好的自我
    2021-02-13 05:12

    getJSONObject("child2");
    

    Will throw an exception if child2 does not exist. Try this instead:

    getchild2 = jArray.getJSONObject(j).optJSONObject("child2");
    

    That way you don't have to catch an exception if child2 doesn't exist and can instead check for null.

提交回复
热议问题