durgaprasad kalapati
durgaprasad kalapati

Reputation: 391

Unable parse the json data in android even by following the actual procedure

iam trying to parse the json data which is shown below.i got output for other Json response by following this procedure but for this i cant get data.

{
"totalResults": 7,
"startIndex": 1,
"hasMoreResults": false,
"itemsPerPage": 10,
"results": [
    {
        "offering_temp_id": {
            "displayName": "Cool Course",
            "id": "cours000000000004841",
            "course_no": "00006081",
            "version": null
        },
        "delivery_id": {
            "displayName": "Instructor-Led",
            "id": "eqcat000000000000004"
        },
        "student_id": {
            "id": "emplo000000006156648",
            "displayName": "Venkat Rao",
            "person_no": "VRAO"
        },
        "reg_no": "00008341",
        "wlist_on": "2017-08-17T08:59:39.843-0400",
        "wlist_priority": 5,
        "Max_Count": null,
        "Current_Count": null,
        "is_no_show": false,
        "is_walk_in": false,
        "offering_action_id": {
            "id": "ofapr000000000013441",
            "displayName": "00009081"
        },
        "class_id": {
            "id": "class000000000006981",
            "displayName": "Cool Course"
        },
        "elements_to_complete": 0,
        "status": "100",
        "id": "regdw000000000012581"
    },
    // total 7 fields
],
"facets": []

}

And iam using the parser procedure as follows

public class EnrollmentParser {
public ArrayList<EnrollmentData> getData(String respnose)//EnrollmentData is my pojo class contains 4 strings and its getters and setters
{
    ArrayList<EnrollmentData> dataList = new ArrayList<>();
    try {
        JSONObject mainObj = new JSONObject(respnose);
        JSONArray array = mainObj.getJSONArray("results");
        for(int i = 0;i<array.length();i++)
        {
            EnrollmentData data = new EnrollmentData();
            JSONObject resObj = array.getJSONObject(i);
            data.setReg_num(resObj.getString("reg_no"));
            data.setElements_to_complete(resObj.getString("elements_to_complete"));
            data.setW_list_on(resObj.getString("wlist_on"));
            data.setW_list_priority(resObj.getString("wlist_priority"));
            dataList.add(data);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


    return dataList;
}

}

when iam trying to display the dataList returned from above method i didnt get any data i got response like

This is the toast response of the dataList returned from method

Suggest me any changes that are required to get the response

Upvotes: 1

Views: 47

Answers (2)

Rice
Rice

Reputation: 155

Why don't you use Gson to convert json into object easily instead of getting the field and set them one by one?

You may check out https://github.com/google/gson

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

There is no faulty parsing code, everything is fine (although better use optString).

By default , the toString function will return the type '@' reference value so

You need to override toString in EnrollmentData class to see the actual content inside EnrollmentData object

You can also collect your list objects as single string using

String str = Arrays.toString(yourList.toArray());

How to print out all the elements of a List in Java?

Upvotes: 2

Related Questions