我有来自服务器的数据:
{
"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"
}]
}
但我想从array of object
获得价值..就像我想从incident__task_i_d
获得价值所以我怎么能得到?
我有一些像这样的代码:
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);
}
}
但是没有正常工作,我有这样的错误:
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)
我是Android Programming
中的新人。
答案 0 :(得分:0)
我建议使用这种方法!!我希望这可以帮到你:)
JsonParser jsonParser = new JsonParser();
JsonObject data=jsonParser.parse(rawContent).getAsJsonObject().getAsJsonArray("data").get(0)
.getAsJsonObject();
String incident= data.get("incident__task_i_d").getAsString();
答案 1 :(得分:0)
很简单,你需要提前知道路径和索引来查询json-data。
JSONObject response=new JSONObject(rawContent);
JSONArray data=response.getJSONArray("data");
String incident=data.getJSONObject(0).getString("incident__task_i_d");