Parser Json volley request

3

You see, I have a Json that works correctly for me being this:

   {
    "codCompeticion": "284",
    "nomCompeticion": "2ª.Cadete I \"Copa Coca Cola\" Valencia",
    "jornadas": [
      {
        "jornada": "1",
        "idClubLocal": "0201211",
        "idEquipoLocal": "0201211301",
        "nomLocal": "Meliana C.F. \"A\"",
        "idClubVisitante": "0201045",
        "idEquipoVisitante": "0201045301",
        "nomVisitante": "Rafelbuñol C.F.",
        "resLocal": "0",
        "resVisitante": "3",
        "fecha": "17/10/2015",
        "hora": "10:30",
        "codInfo": "81769",
        "escudoLocal": "./img/logosClubes/0201211.jpg",
        "escudoVisitante": "./img/logosClubes/0201045.jpg"
      },
      {
        "jornada": "2",
        "idClubLocal": "0201045",
        "idEquipoLocal": "0201045301",
        "nomLocal": "Rafelbuñol C.F.",
        "idClubVisitante": "0201573",
        "idEquipoVisitante": "0201573211",
        "nomVisitante": "F.B. Alfara Del Patriarca",
        "resLocal": "0",
        "resVisitante": "10",
        "fecha": "24/10/2015",
        "hora": "12:30",
        "codInfo": "123322",
        "escudoLocal": "./img/logosClubes/0201045.jpg",
        "escudoVisitante": "./img/logosClubes/0201573.jpg"

      }
    ]
  }

And this is the code to parse it:

public List<Calendario_Volley> parseJson(JSONObject jsonObject){
    // Variables locales
    List<Calendario_Volley> rankingAmonestacionesCadetes = new ArrayList<>();
    JSONArray jsonArray= null;

    try {
        // Obtener el array del objeto
        jsonArray = jsonObject.getJSONArray("jornadas");

        for(int i=0; i<jsonArray.length(); i++){

            try {
                JSONObject objeto= jsonArray.getJSONObject(i);

                Calendario_Volley RankingAmonestacionesCadetes = new Calendario_Volley(
                        ("JORNADA - ")+objeto.getString("jornada"),
                        ("FECHA - ")+objeto.getString("fecha"),
                        objeto.getString("nomLocal"),
                        ("HORA - ")+objeto.getString("hora"),
                        objeto.getString("nomVisitante"),
                        objeto.getString("resVisitante"),
                        objeto.getString("resLocal")+(" - "),
                        ("ARBITRO - ")+objeto.getString("arbitro"),

                        objeto.getString("escudoLocal"),
                        objeto.getString("escudoVisitante")+(" - "));

But how is it done to parse it if it starts in square bracket?

[
  {
    "codCompeticion": "284",
    "nomCompeticion": "2ª.Cadete I \"Copa Coca Cola\" Valencia",
    "jornadas": [
      {
        "jornada": "1",
        "idClubLocal": "0201211",
        "idEquipoLocal": "0201211301",
        "nomLocal": "Meliana C.F. \"A\"",
        "idClubVisitante": "0201045",
        "idEquipoVisitante": "0201045301",
        "nomVisitante": "Rafelbuñol C.F.",
        "resLocal": "0",
        "resVisitante": "3",
        "fecha": "17/10/2015",
        "hora": "10:30",
        "codInfo": "81769",
        "escudoLocal": "./img/logosClubes/0201211.jpg",
        "escudoVisitante": "./img/logosClubes/0201045.jpg"
      },
      {
        "jornada": "2",
        "idClubLocal": "0201045",
        "idEquipoLocal": "0201045301",
        "nomLocal": "Rafelbuñol C.F.",
        "idClubVisitante": "0201573",
        "idEquipoVisitante": "0201573211",
        "nomVisitante": "F.B. Alfara Del Patriarca",
        "resLocal": "0",
        "resVisitante": "10",
        "fecha": "24/10/2015",
        "hora": "12:30",
        "codInfo": "123322",
        "escudoLocal": "./img/logosClubes/0201045.jpg",
        "escudoVisitante": "./img/logosClubes/0201573.jpg"

      }
    ]
  }
]
    
asked by Rafel C.F 10.04.2016 в 22:50
source

2 answers

1

There is something important to consider,

The answer .Json can be of two types:

- If the .json starts with { it is considered as a Json object.

- If the .json starts with [ it is considered as a Json fix.

So in your case you would get the json objects from the json Array in this way:

JSONArray jsonArray = new JSONArray(RespuestaJson);
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jornada = jsonArray.getJSONObject(i);
    //De esta forma obtendrías los valores del objeto Jornada.
     String idClubLocal = jornada.getString("idClubLocal");
     String idEquipoLocal = jornada.getString("idEquipoLocal");
     String nomLocal = jornada.getString("nomLocal");
     String idClubVisitante = jornada.getString("idClubVisitante");
     String idEquipoVisitante = jornada.getString("idEquipoVisitante");

}
    
answered by 11.04.2016 / 00:00
source
0

If it has brackets it means that it is an array, so you should pause it as such, probably you had something like this before:

JSONObject jsonobject = new JSONObject(jsonString);

Now you have to do

JSONArray raiz = new JSONArray(jsonString);

and traverse the array for each object

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = raiz.getJSONObject(i);

after that your code remains the same

    
answered by 10.04.2016 в 23:08