2015-04-22 45 views
-3

我有了這個解析器:的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"]}]} 

但它給我的每一個列表中的行查看相同的段落數據,所以如果有人知道如何解決這個問題,應該對他非常好!謝謝 !

+0

使用GSON並創建一個只讀「horaires」的類 如果你想使用GSON,我會發布這個答案和更多的細節 –

+0

這不是所有的代碼我只是這部分!我使用凌空它工作得很好,我不想創建另一個類只爲horaires,因爲小時工作的解析已經很好:/ –

+0

創建新的listdata不使用listdata.clear(); –

回答

0

使用gson lib(它是谷歌圖書館),它是解析任何jsonObject的最好方法和簡單。 Gson是一個Java庫,可用於將Java對象轉換爲其JSON表示。它也可以用來將JSON字符串轉換爲等效的Java對象。 Gson可以使用任意Java對象,包括您沒有源代碼的預先存在的對象。

https://code.google.com/p/google-gson/https://github.com/google/gson

例如:

Map jsonJavaRootObject = new Gson().fromJson("{/*whatever your mega complex object*/}", Map.class) 
+0

請檢查第一和第二個評論的問題 –

+0

我回答之前這些評論或我沒有看到它......但。我給了他他可以在哪裏找到gson以及誰在使用它...... – MilanNz

0

變化:ArrayList<String> listdata = new ArrayList<String>();List<Horaires> listdata = new ArrayList<Horaires>();

0

,您可以輕鬆簡單地寫幾行代碼解析JSON。請在下面找到我已經做了充分利用您的JSON的數據結構,其定義如下數據的代碼:

public class JsonDataVo { 
    String codeArret; 
    String libelle; 
    boolean accessible; 
    int numLigne; 
    String directionSens1; 
    String directionSens2; 
    boolean ligneAccessible; 
    int etatTrafic; 
    String libelleTrafic; 
    String codeCouleur; 
    String plageDeService; 
    ArrayList<HeureDataVO> horaires = new ArrayList<HeureDataVO>(); 
    ArrayList<HeureDataVO> prochainsHoraires = new ArrayList<HeureDataVO>(); 

    public String getCodeArret() { 
     return codeArret; 
    } 
    public void setCodeArret(String codeArret) { 
     this.codeArret = codeArret; 
    } 
    public String getLibelle() { 
     return libelle; 
    } 
    public void setLibelle(String libelle) { 
     this.libelle = libelle; 
    } 
    public boolean getAccessible() { 
     return accessible; 
    } 
    public void setAccessible( boolean accessible) { 
     this.accessible = accessible; 
    } 
    public int getNumLigne() { 
     return numLigne; 
    } 
    public void setNumLigne(int numLigne) { 
     this.numLigne = numLigne; 
    } 
    public String getDirectionSens1() { 
     return directionSens1; 
    } 
    public void setDirectionSens1(String directionSens1) { 
     this.directionSens1 = directionSens1; 
    } 
    public String getDirectionSens2() { 
     return directionSens2; 
    } 
    public void setDirectionSens2(String directionSens2) { 
     this.directionSens2 = directionSens2; 
    } 
    public boolean isLigneAccessible() { 
     return ligneAccessible; 
    } 
    public void setLigneAccessible(boolean ligneAccessible) { 
     this.ligneAccessible = ligneAccessible; 
    } 
    public int getEtatTrafic() { 
     return etatTrafic; 
    } 
    public void setEtatTrafic(int etatTrafic) { 
     this.etatTrafic = etatTrafic; 
    } 
    public String getLibelleTrafic() { 
     return libelleTrafic; 
    } 
    public void setLibelleTrafic(String libelleTrafic) { 
     this.libelleTrafic = libelleTrafic; 
    } 
    public String getCodeCouleur() { 
     return codeCouleur; 
    } 
    public void setCodeCouleur(String codeCouleur) { 
     this.codeCouleur = codeCouleur; 
    } 
    public String getPlageDeService() { 
     return plageDeService; 
    } 
    public void setPlageDeService(String plageDeService) { 
     this.plageDeService = plageDeService; 
    } 
    public ArrayList<HeureDataVO> getHoraires() { 
     return horaires; 
    } 
    public void setHoraires(ArrayList<HeureDataVO> horaires) { 
     this.horaires = horaires; 
    } 
    public ArrayList<HeureDataVO> getProchainsHoraires() { 
     return prochainsHoraires; 
    } 
    public void setProchainsHoraires(ArrayList<HeureDataVO> prochainsHoraires) { 
     this.prochainsHoraires = prochainsHoraires; 
    } 

} 

public class HeureDataVO { 
    String heure; 
    ArrayList<Integer> passages = new ArrayList<Integer>(); 

    public String getHeure() { 
     return heure; 
    } 
    public void setHeure(String heure) { 
     this.heure = heure; 
    } 
    public ArrayList<Integer> getPassages() { 
     return passages; 
    } 
    public void setPassages(ArrayList<Integer> passages) { 
     this.passages = passages; 
    } 
} 

public ArrayList<Integer> getPassages(JSONObject jsonObj){ 
     ArrayList<Integer> tmpValues = new ArrayList<Integer>(); 
     try { 
      JSONArray arrayOfValues = jsonObj.getJSONArray("passages"); 
      for(int i = 0; i < arrayOfValues.length(); i++){ 
       tmpValues.add(arrayOfValues.getInt(i)); 
      } 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return tmpValues; 
    } 

public JsonDataVo parseJson(JSONObject response){ 
     JsonDataVo jsonDataVo = new JsonDataVo(); 
     try { 
      JSONObject arretobj = response.getJSONObject("arret"); 
      jsonDataVo.setCodeArret(arretobj.getString("codeArret")); 
      jsonDataVo.setLibelle(arretobj.getString("libelle")); 
      jsonDataVo.setAccessible(arretobj.getBoolean("accessible")); 
      JSONObject lingeObject = response.getJSONObject("ligne"); 
      jsonDataVo.setNumLigne(lingeObject.getInt("numLigne")); 
      jsonDataVo.setDirectionSens1(lingeObject.getString("directionSens1")); 
      jsonDataVo.setDirectionSens2(lingeObject.getString("directionSens2")); 
      jsonDataVo.setLigneAccessible(lingeObject.getBoolean("accessible")); 
      jsonDataVo.setEtatTrafic(lingeObject.getInt("etatTrafic")); 
      jsonDataVo.setLibelleTrafic(lingeObject.getString("libelleTrafic")); 
      jsonDataVo.setCodeCouleur(response.getString("codeCouleur")); 
      jsonDataVo.setPlageDeService(response.getString("plageDeService")); 
      JSONArray horairesArray = response.getJSONArray("horaires"); 
      for(int i = 0; i < horairesArray.length(); i++){ 

       JSONObject horairesJsonObject = horairesArray.getJSONObject(i); 
        HeureDataVO tmp = new HeureDataVO(); 
        tmp.setHeure(horairesJsonObject.getString("heure")); 
        tmp.setPassages(getPassages(horairesJsonObject)); 
        jsonDataVo.getHoraires().add(tmp); 
      } 

      JSONArray prochainsHorairesArray = response.getJSONArray("horaires"); 
      for(int i = 0; i < prochainsHorairesArray.length(); i++){ 

       JSONObject horairesJsonObject = prochainsHorairesArray.getJSONObject(i); 
        HeureDataVO tmp = new HeureDataVO(); 
        tmp.setHeure(horairesJsonObject.getString("heure")); 
        tmp.setPassages(getPassages(horairesJsonObject)); 
        jsonDataVo.getProchainsHoraires().add(tmp); 
      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return jsonDataVo; 
    } 

我希望這將幫助你得到你想要的解決方案,你會不會需要任何庫來解析json。