【发布时间】:2015-06-30 01:10:04
【问题描述】:
我有这个解析器:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
ArrayList<String> listdata = new ArrayList<String>();
JSONArray configJsonArray = response.getJSONArray("horaires");
for(int configIterator = 0; configIterator < configJsonArray.length(); configIterator++){
Horaires horaires = new Horaires();
JSONObject innerConfigObj = configJsonArray.getJSONObject(configIterator);
configGrade = innerConfigObj.getString("heure");
horaires.setHeure(configGrade);
listdata.clear();
JSONArray jr = configJsonArray.getJSONObject(configIterator).getJSONArray("passages");
for(int v = 0; v < jr.length(); v++){
listdata.add(jr.getString(v));
horaires.setPassages(listdata);
}
horairesList.add(horaires);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
}
});
我希望他解析这种类型的 Json:
{"arret":{"codeArret":"BRTA1","libelle":"Bretagne","accessible":true},"ligne":{"numLigne":"3","directionSens1":"Marcel Paul","directionSens2":"Neustrie","accessible":true,"etatTrafic":1,"libelleTrafic":"Service normal"},"codeCouleur":"J","plageDeService":"Des horaires adapt\u00e9s aux rythmes de vos journ\u00e9es\r\nEn p\u00e9riode scolaire...\r\nJour rose : du lundi au vendredi (hors 10 mai).\r\nJour vert : les samedis.\r\nJour bleu : les dimanches et jours f\u00e9ri\u00e9s (aucun service le 1er mai).\r\nPendant les vacances scolaires...\r\nJour jaune : du lundi au vendredi (hors vacances de Toussaint).","horaires":[{"heure":"5h","passages":["14","34","54"]},{"heure":"6h","passages":["12","21","31","40","49","58"]},{"heure":"7h","passages":["05","12","19","26","33","40","46","53"]},{"heure":"8h","passages":["00","07","14","21","28","35","42","49","56"]},{"heure":"9h","passages":["03","10","17","23","30","37","44","51","58"]},{"heure":"10h","passages":["05","12","19","26","33","40","47","54"]},{"heure":"11h","passages":["01","08","15","22","29","36","43","50","57"]},{"heure":"12h","passages":["04","11","17","24","31","37","44","53"]},{"heure":"13h","passages":["00","06","13","19","25","31","37","43","49","55"]},{"heure":"14h","passages":["01","07","13","19","25","31","37","43","49","55"]},{"heure":"15h","passages":["01","07","13","19","25","30","36","42","48","54"]},{"heure":"16h","passages":["00","06","12","18","24","30","36","42","48","54"]},{"heure":"17h","passages":["00","06","13","19","25","31","37","43","49","54"]},{"heure":"18h","passages":["00","06","12","18","24","30","36","42","48","54"]},{"heure":"19h","passages":["00","06","12","19","26","33","39","43","49","54"]},{"heure":"20h","passages":["01","09","13","21","30","39","43","52"]},{"heure":"21h","passages":["01","10","20","30","40","50"]},{"heure":"22h","passages":["00","10","20","32","42"]},{"heure":"23h","passages":["02","32"]},{"heure":"0h","passages":["02","32"]}],"prochainsHoraires":[{"heure":"22h","passages":["00"]},{"heure":"22h","passages":["10"]}]}
但它在列表的每一行都给了我查看相同的段落数据,所以如果有人知道如何解决这个问题,他会非常好!谢谢!
【问题讨论】:
-
使用 GSON 并创建一个只读“horaires”的类
-
这不是我剪掉的所有代码!我使用 volley 效果很好,我不想只为 horaires 创建另一个课程,因为时间的解析已经很好了:/
-
创建新的 listdata 不要使用 listdata.clear();
标签: android json list parsing object