Fania Indah Fania Indah - 3 months ago 9
Android Question

Not a JSON Object

i have data from server like this :

{
"error": false,
"alerts": {
"code": "200",
"message": "retrieve success"
},
"data": [{
"incident__task_i_d": "8d37a3f1-d257-4833-9701-4eab0e48076f",
"address": "Pulo Jahe Motor",
"task_status": "Completed",
"incident_i_d": "83eeed0f-4ea4-416b-8721-637e40ac9f72",
"vehicle_i_d": "36420155-6214-4745-bbf3-7dae7d50ce9c",
"agent_i_d": "1c3436fd-9338-4910-9686-8c1cd0a33433",
"dispatched_time": "2016-08-12T08:29:47.89",
"origin_latitude": "-6.8979664",
"origin_longitude": "107.6135752",
"destination_latitude": "-6.198816382772501",
"destination_longitude": "106.921986633551",
"created_dt": "2016-08-12T08:29:47.89",
"departed_time": "2016-08-12T10:13:20.947",
"arrival_time": "2016-08-12T10:11:14.93",
"kick_off_time": "2016-08-12T10:13:54.54",
"finished_time": "2016-08-12T10:14:30.893",
"incident_reported_dt": "2016-08-11T08:21:34.23"
}]
}

but i want to get value from array of objek .. example i want to get value from incident__task_i_d . how can i get ?
i have a some code like this :

try
{
rawContent = new String(content);
Log.i("TAG", "ALL DATA "+rawContent);

JsonObject json = new JsonParser().parse(rawContent).getAsJsonObject();
Gson gson = new GsonBuilder().setDateFormat(ModelRekap.DATE_FORMAT).create();

Log.i("haha","masuk list ini");
JsonArray jaray = json.getAsJsonArray("data");

listInfoAnda = new ArrayList<>();
for (int i = 0; i < jaray.size(); i++) {
Log.i("TAG", "ARRAY " + i);
try {
JsonElement address = ((JsonObject)jaray.get(i)).get("address");
Log.i("ISI rekap address", "" + address.toString());
ModelRekap mCoba = gson.fromJson(jaray.get(i), ModelRekap.class);

listInfoAnda.add(mCoba);
Log.i("TAG","MODEL" + mCoba.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

onFinishRequest(true, rawContent);
}
catch(Exception e)
{
e.printStackTrace();
onFinishRequest(false, rawContent);
}
}


but doesn't work correctly , i have some error like this :

08-12 22:31:03.926 6111-6111/com.example.fania.cobalagi W/System.err: java.lang.IllegalStateException: Not a JSON Object: [{"incident__task_i_d":"8d37a3f1-d257-4833-9701-4eab0e48076f","address":"Pulo Jahe Motor","task_status":"Completed","incident_i_d":"83eeed0f-4ea4-416b-8721-637e40ac9f72","vehicle_i_d":"36420155-6214-4745-bbf3-7dae7d50ce9c","agent_i_d":"1c3436fd-9338-4910-9686-8c1cd0a33433","dispatched_time":"2016-08-12T08:29:47.89","origin_latitude":"-6.8979664","origin_longitude":"107.6135752","destination_latitude":"-6.198816382772501","destination_longitude":"106.921986633551","created_dt":"2016-08-12T08:29:47.89","departed_time":"2016-08-12T10:13:20.947","arrival_time":"2016-08-12T10:11:14.93","kick_off_time":"2016-08-12T10:13:54.54","finished_time":"2016-08-12T10:14:30.893","incident_reported_dt":"2016-08-11T08:21:34.23"}]
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.example.fania.cobalagi.api.ApiRekapData$1.onSuccess(ApiRekapData.java:100)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.radyalabs.async.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:264)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.radyalabs.async.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:134)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at android.os.Looper.loop(Looper.java:146)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5593)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-12 22:31:03.931 6111-6111/com.example.fania.cobalagi W/System.err: at dalvik.system.NativeStart.main(Native Method)

can anybody help me ? i'm new in android proegrammer . Thank's

Answer

i suggest to use this approach !! i hope this help you :)

    JsonParser jsonParser = new JsonParser();
    JsonObject data=jsonParser.parse(rawContent).getAsJsonObject().getAsJsonArray("data").get(0)
        .getAsJsonObject();

    String incident= data.get("incident__task_i_d").getAsString();
Comments