简体   繁体   中英

How to handle missing key in JSON when using Retrofit Library to fetch data android

I'm using efficient retrofit library to get data. The json i am getting through api is as given below:

[
{
    "doctor_detail": [
        {
            "experience_years": null,
            "clinic": [
                {
                    "price": 163,
                    "main_facility": {
                        "name": "West Delhi Dental Clinic",
                        "address": {
                            "mobile": "+911166107201",
                            "latitude": null,
                            "longitude": null,
                            "locality": {
                                "city": {
                                    "name": "Delhi"
                                },
                                "name": "Janak Puri"
                            }
                        }
                    }
                }
            ],
            "specialities": [
                {
                    "name": "Dentist",
                    "slug": "dentist"
                }
            ],
            "owner": {
                "first_name": "Bhudev",
                "last_name": "Sharma",
                "middle_name": null
            },
            "logo": [],
            "doctor_id": "286207",
            "education": []
        },
        {
            "experience_years": null,
            "clinic": [],
            "specialities": [],
            "owner": {
                "first_name": "Hema",
                "last_name": "Anand",
                "middle_name": null
            },
            "logo": [],
            "doctor_id": "12137",
            "education": []
        }
    ]
},
{
    "last_page": 2
},
{
    "title": "dentist"
}]

In First object of json i am getting an array with key doctor_detail having two bjects in first object i am getting an array with key clinic having price key and an array main_facility having some other keys like name,address.

but in second object of doctor_detail i am getting same clinic key but don't having any key inside clinic due to this i am getting a run time error :

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)

in RecyclerAdapter when i am going to get price for second object from ArrayList<String> mPriceArrayList .

So please tell me if there is any way to handle such kind of problem. Thanks

Finally i got answer this is due to wrong json. It can not be handle.

Dear Ravi i think its not your problem you are getting wrong JSON but still you can handle it by using try-catch block in adapter where you are getting ArrayIndexOutOfBounds Exception and you can set any value in text view in catch block for which you are not getting keys like:

    try{
  mPriceTextView.setText(mPriceArrayList.get(getAdapterPosition()));
}catch(Exception oe){
  mPriceTextView.setText("price is missing");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM