我有了這個解析器:的Android解析字符串JsonArray不工作
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」的類 如果你想使用GSON,我會發布這個答案和更多的細節 –
這不是所有的代碼我只是這部分!我使用凌空它工作得很好,我不想創建另一個類只爲horaires,因爲小時工作的解析已經很好:/ –
創建新的listdata不使用listdata.clear(); –