0

Hello all i have a problem with a special json formatted with an array of array and object.

{
  "contrat":  [
    {
      "id":0,
      "email":"RAJita@YAHOO.COM",
      "mobile":"55281055",
      "fixe":"33740970",
      "numContrat":"252010176",
      "offre":"bb : Automobile",
      "dateDebut":"2013-01-03",
      "dateFin":"2014-01-03",
      "statut":"En cours"
    },
    [ ],
    [ ]
  ],
  "success":1,
  "code":200
}

Normally if I had a normal json with an array of object, I used this method:

JSONArray contratObj = json.getJSONArray("contrat"); // JSONArray
for (int i = 0; i < contratObj.length(); i++) {
    // get first contrat object from JSON Array
    JSONObject contrat = contratObj.getJSONObject(i);               

    contratlist.add(new contratitem(contrat.getInt("id"),
    contrat.getString("numContrat"), contrat.getString("offre"), 
    contrat.getString("dateDebut"), contrat.getString("dateFin"), 
    contrat.getString("statut")));
}

but the array "contrat" contains empty array and object

How can I change my code to ignore empty array ?

slama007
  • 1,273
  • 2
  • 18
  • 34

4 Answers4

0

You can use it like this:

JSONObject contrat = contratObj.getJSONObject(i);
if(contrat.length!=0) {
contratlist.add(new contratitem(contrat.getInt("id"),
contrat.getString("numContrat"), contrat.getString("offre"), 
contrat.getString("dateDebut"), contrat.getString("dateFin"), 
contrat.getString("statut")));
}
Archit Goel
  • 694
  • 5
  • 19
0

first validate your JSON here: http://jsonlint.com/

Than, I would use http://www.jsonschema2pojo.org/ . mind to select gson as a parser and json as real data, instad of schema, because you copying a real JSON data. You can download you class as JAR or copy the source. After it you can parse the JSON with GSON and load into classes.

Retrofit is a good way to do it. It's a very simple downloading library.

Any further questions, please add as comment. Take care

narancs
  • 5,234
  • 4
  • 41
  • 60
0

Try like this, it should work.

JSONArray contratObj = json.optJSONArray("contrat"); // JSONArray
        if (contratObj != null) {
          for (int i = 0; i < contratObj.length(); i++) {
            // get first contrat object from JSON Array
            JSONObject contrat = contratObj.optJSONObject(i);

            if (contrat != null) {
              contratlist.add(new contratitem(contrat.getInt("id"), contrat.optString("numContrat"),
                  contrat.getString("offre"), contrat.optString("dateDebut"),
                  contrat.getString("dateFin"), contrat.optString("statut")));
            }
          }
        }

read above Document

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

With the current code and json that you have, the App would crash with JSONException as it won't be able to execute JSONObject contrat = contratObj.getJSONObject(i); for the 2nd element as it is an array. So, you need to check whether the element is a JSONArray or JSONObject before that. Modifying your code as below will help you achieve that and to ignore JSONArrays.

JSONArray contratObj = json.getJSONArray("contrat");
for (int i = 0; i < contratObj.length(); i++) {
    if (contratObj.get(i) instanceof JSONObject) {
        JSONObject contrat = contratObj.getJSONObject(i);
        contratlist.add(new contratitem(contrat.getInt("id"),
        contrat.getString("numContrat"), contrat.getString("offre"), 
        contrat.getString("dateDebut"), contrat.getString("dateFin"), 
        contrat.getString("statut")));
    }
}

Let me know if this helps !

Suhas
  • 1,451
  • 14
  • 22