My app fetches data from a Google Cloud Endpoint backend and this data is in json format.When I view this data in a ListView, this is what I get:
which is expected since I haven't done any form of parsing or conversion. My target output is this:
I have searched and searched for a way to do this with no luck. One example uses a different method and IMO is way too complicated for something I feel shouldn't take more than 5 lines of code.Another reads its data from a file and so do several others.
This is from the AsyncTask where am making the call to the endpoint:
@Override
protected void onPostExecute(List<Student> result) {
ArrayAdapter<Student> arrayAdapter = new ArrayAdapter<Student>(getApplicationContext(), R.layout.row_text, result);
studentListView.setAdapter(new ArrayAdapter<Student>(getApplicationContext(), R.layout.activity_endpoint_launcher, result));
studentListView.setAdapter(arrayAdapter);
}
I tried adding this:
String jsString = result.toString();
try {
JSONObject jsonObject = new JSONObject(jsString);
String name = jsonObject.getString(TAG_NAME);
Log.d("EndpointLauncher", name);
} catch (JSONException e) {
e.printStackTrace();
}
but I get the error (which doesn't crash the app though):
W/System.err﹕ org.json.JSONException: Value [{"grade":50,"matriculationNumber":"453","name":"Vera Brown"},{"grade":90,"matriculationNumber":"123456","name":"Sam Sung"},{"grade":90,"matriculationNumber":"654321","name":"To Shiba"},{"grade":90,"matriculationNumber":"876123","name":"Ann Droid"}] of type org.json.JSONArray cannot be converted to JSONObject
Any help would be greatly appreciated.