0

i am trying to parse Json data using bean classes as below, but i am getting the error "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT"

JSON data

{
"response": true,
"message": "Success",
"Data": [
    {
        "laboratory_id": 1000,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "Laboratory & Research Institute",
        "laboratory_address": "Cross Road, ",
        "laboratory_rating": 2.5,
        "laboratory_profile": {
            "head_name": "Ramesh Jain",
            "degree_name": "M.D",
            "speciality": "Lab Technology",
            "license_number": "34343434",
            "laboratory": "Laboratory"
        }
    },
    {
        "laboratory_id": 1001,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "XYZ LAB",
        "laboratory_address": "ASHKA ROAD",
        "laboratory_rating": 0,
        "laboratory_profile": {
            "head_name": "MR X",
            "degree_name": "MBBS",
            "speciality": null,
            "license_number": "123456",
            "laboratory": "Diagnostic"
        }
    }

]

}

LaboratoryListModel

public class LaboratoryListModel {

private String response;
private String message;
private ArrayList<LaboratoryList> Data = new ArrayList<LaboratoryList>();


public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ArrayList<LaboratoryList> getData() {
    return Data;
}

public void setData(ArrayList<LaboratoryList> data) {
    Data = data;
}

}

LaboratoryList
public class LaboratoryList {

private String laboratory_id;
private String laboratory_image;
private String laboratory_name;
private String laboratory_address;
private String laboratory_rating;

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>();

public ArrayList<LaboratoryProfile> getLaboratory_profile() {
    return laboratory_profile;
}

public void setLaboratory_profile(
        ArrayList<LaboratoryProfile> laboratory_profile) {
    this.laboratory_profile = laboratory_profile;
}

public String getLaboratory_id() {
    return laboratory_id;
}

public void setLaboratory_id(String laboratory_id) {
    this.laboratory_id = laboratory_id;
}

public String getLaboratory_image() {
    return laboratory_image;
}

public void setLaboratory_image(String laboratory_image) {
    this.laboratory_image = laboratory_image;
}

public String getLaboratory_name() {
    return laboratory_name;
}

public void setLaboratory_name(String laboratory_name) {
    this.laboratory_name = laboratory_name;
}

public String getLaboratory_address() {
    return laboratory_address;
}

public void setLaboratory_address(String laboratory_address) {
    this.laboratory_address = laboratory_address;
}

public String getLaboratory_rating() {
    return laboratory_rating;
}

public void setLaboratory_rating(String laboratory_rating) {
    this.laboratory_rating = laboratory_rating;
}

}

LaboratoryProfile

public class LaboratoryProfile {

private String head_name;
private String degree_name;
private String speciality;
private String license_number;
private String laboratory;

public String getHead_name() {
    return head_name;
}

public void setHead_name(String head_name) {
    this.head_name = head_name;
}

public String getDegree_name() {
    return degree_name;
}

public void setDegree_name(String degree_name) {
    this.degree_name = degree_name;
}

public String getSpeciality() {
    return speciality;
}

public void setSpeciality(String speciality) {
    this.speciality = speciality;
}

public String getLicense_number() {
    return license_number;
}

public void setLicense_number(String license_number) {
    this.license_number = license_number;
}

public String getLaboratory() {
    return laboratory;
}

public void setLaboratory(String laboratory) {
    this.laboratory = laboratory;
}

}

Webservice

public class GetLaboratoryListTask extends
        AsyncTask<String, Integer, Object> {

    private final Context mContext;
    private final ProgressDialog mProgressDialog;
    private ListView mlistView;

    LaboratoryListModel labListModel;

    public GetLaboratoryListTask(final Context mContext, ListView listView) {
        this.mContext = mContext;
        mlistView = listView;
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setMessage("Please wait..");
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    @Override
    protected Object doInBackground(String... params) {

        LaboratoryListModel laboratoryListModel = (LaboratoryListModel) getLaboratoryList(params[0]);
        if (laboratoryListModel != null)
            return laboratoryListModel.getData();
        else
            return null;

    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }

        if (result != null) {
            if (labListModel.getResponse().equalsIgnoreCase("true")) {
                if (result != null && result instanceof List) {
                    mlistView.setAdapter(new SearchLabAdapter(
                            LabListActivity.this,
                            (List<LaboratoryList>) result));
                }
            } else {

            }
        }

    }

    private Object getLaboratoryList(String category_id) {
        String webUrl = Constant.URL_SEARCH_LABORATORY;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(),
                    10000);
            HttpResponse response;
            JSONObject object = new JSONObject();
            JSONObject objectContacts = new JSONObject();
            try {
                HttpPost post = new HttpPost(Constant.URL_SEARCH_LABORATORY);

                objectContacts.put(Constant.CITY, "");
                objectContacts.put(Constant.LOCATION_ZIPCODE, "");
                objectContacts.put(Constant.LABORATORY_NAME, "");

                StringEntity se = new StringEntity(
                        objectContacts.toString());

                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                post.setEntity(se);
                response = client.execute(post);

                if (response != null) {
                    //
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    String json1 = reader.readLine();
                    JSONTokener tokener = new JSONTokener(json1);
                    jObject = new JSONObject(tokener);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            labListModel = (LaboratoryListModel) new Gson().fromJson(
                    jObject.toString(), LaboratoryListModel.class);

            return labListModel;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

As i am newbie, i don't know what's the problem, i did google but i didn't got solution, can anyone help me please?

2
  • Hi Vivek, did you checked your response? is it valid Json, it seems its valid even though i would suggest check it at jsonlint.org once. Commented Aug 13, 2015 at 9:09
  • yes i did checked at jsonlint.org. it is valid JSON. Commented Aug 13, 2015 at 9:11

3 Answers 3

2

Regarding your comment. You need to change your model. Your LaboratoryList should contain only one profile.

Change it to:

private LaboratoryProfile laboratory_profile;

public LaboratoryProfile getLaboratory_profile() {
    return laboratory_profile;
}
Sign up to request clarification or add additional context in comments.

Comments

1

change your LaboratoryList class line

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>();

to this

private LaboratoryProfile laboratory_profile;

and make remove the previous getters and setters with new one .and you are done

Comments

0

In your LaboratoryList.java you have a list of profiles, but your JSON does not represent this. It is an JSON object. Change this to an array or make the list of profile only a single profile.

Your JSON for profile should look like this:

"laboratory_profile":[  
        {  
           "head_name":"MR X",
           "degree_name":"MBBS",
           "speciality":null,
           "license_number":"123456",
           "laboratory":"Diagnostic"
        }
     ]

2 Comments

can you please help me how can i do it? as i already mentioned i am a newbie so i don't have much idea.
is there any other way ? without changing JSON can we do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.