XCODE Parse Json with 4 arrays inside

Discussion in 'Mac Programming' started by joak, Mar 7, 2014.

  1. joak, Mar 7, 2014
    Last edited by a moderator: Mar 7, 2014

    joak macrumors newbie

    Joined:
    Mar 7, 2014
    #1
    Hello guys.

    I have NSDictionary object with the data attached file(JSON format), I have to insert all data into tables, each level(array) it's a diferent table, when I run the code I get in console this..

    Code:
    2014-03-07 08:55:38.189 prueba2[500:70b] id 10
    2014-03-07 08:55:38.190 prueba2[500:70b] torneo Juvenil Sabatino
    2014-03-07 08:55:38.191 prueba2[500:70b] id_jornada 11
    2014-03-07 08:55:38.191 prueba2[500:70b] fecha_partido 2014-03-01 09:00:00
    2014-03-07 08:55:38.191 prueba2[500:70b] id_jornada 11
    2014-03-07 08:55:38.192 prueba2[500:70b] fecha_partido 2014-03-01 10:00:00
    2014-03-07 08:55:38.192 prueba2[500:70b] id_jornada 11
    2014-03-07 08:55:38.192 prueba2[500:70b] fecha_partido 2014-03-01 11:00:00
    2014-03-07 08:55:38.193 prueba2[500:70b] id_jornada 11
    2014-03-07 08:55:38.193 prueba2[500:70b] fecha_partido 2014-03-01 12:00:00
    2014-03-07 08:55:38.193 prueba2[500:70b] id_jornada 11
    2014-03-07 08:55:38.194 prueba2[500:70b] fecha_partido 2014-03-01 13:00:00
    2014-03-07 08:55:38.194 prueba2[500:70b] id 6
    2014-03-07 08:55:38.194 prueba2[500:70b] torneo Sabatino Libre
    2014-03-07 08:55:38.194 prueba2[500:70b] id_jornada 1
    2014-03-07 08:55:38.195 prueba2[500:70b] fecha_partido 2014-03-01 16:00:00
    2014-03-07 08:55:38.195 prueba2[500:70b] id_jornada 1
    2014-03-07 08:55:38.195 prueba2[500:70b] fecha_partido 2014-03-01 17:00:00
    2014-03-07 08:55:38.196 prueba2[500:70b] id_jornada 1
    2014-03-07 08:55:38.198 prueba2[500:70b] fecha_partido 2014-03-01 18:00:00
    Why "for (NSDictionary *equipo in equipos)" doesn't work?
     

    Attached Files:

  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post your code.

    And Please use CODE tags.

    Also post the actual JSON text. Not a picture of it in a Viewer, the actual text, inside CODE tags.
     
  3. joak thread starter macrumors newbie

    Joined:
    Mar 7, 2014
    #3
    Code:
    -(void) createDatabase
    {
        NSArray *torneos = [self.arrayMaster objectForKey:@"Cedula"];
        int lint =0;
        int lint1=0;
        for (NSDictionary *torneo in torneos)
        {
            NSLog(@"id %@",torneo[@"id_torneo"]);
            NSLog(@"torneo %@",torneo[@"nom_torneo"]);
            NSDictionary *partidos = [[torneos objectAtIndex: lint] objectForKey: @"Partidos"];
            for (NSDictionary *partido in partidos)
            {
                NSLog(@"id_jornada %@",partido[@"id_jornada"]);
                NSLog(@"fecha_partido %@",partido[@"fecha_partido"]);
                NSDictionary *equipos = [[torneos objectAtIndex:lint] objectForKey: @"Equipos"];
                for (NSDictionary *equipo in equipos)
                {
                    NSLog(@"id_equipo %@",equipo[@"id_equipo"]);
                    NSLog(@"nom_equipo %@",partido[@"nom_equipo"]);
                }
            }
            lint++;
        }
    
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The code hilited in red is probably wrong.

    It's trying to take an object at index lint out of the torneos array. Based on the posted View of the JSON data, there is no "Equipos" array in the data at that point, so equipos is assigned nil, and the for loop runs no iterations.

    It looks like you copied and pasted the line from earlier in the code, but neglected to make all the necessary changes to make it refer to the proper object.
     
  5. joak thread starter macrumors newbie

    Joined:
    Mar 7, 2014
    #5
    If I Print description of "partido" object ..
    Code:
    Printing description of partido:
    {
        Equipos =     (
                    {
                Jugadores =             (
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 6;
                        "jug_playera" = 8;
                        "jug_repre" = N;
                        "jugador_mat" = Soto;
                        "jugador_nom" = "Marco Antonio";
                        "jugador_pat" = Ramirez;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 13;
                        "jug_playera" = 56;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Cesar;
                        "jugador_pat" = " Mendieta";
                    },
                                    {
                        fechareg = "2014-01-10";
                        "id_jugador" = 2;
                        "jug_playera" = 7;
                        "jug_repre" = N;
                        "jugador_mat" = Resendiz;
                        "jugador_nom" = Brandon;
                        "jugador_pat" = Guizar;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 9;
                        "jug_playera" = 4;
                        "jug_repre" = N;
                        "jugador_mat" = Angeles;
                        "jugador_nom" = Ricardo;
                        "jugador_pat" = Alvarez;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 16;
                        "jug_playera" = 117;
                        "jug_repre" = N;
                        "jugador_mat" = Suarez;
                        "jugador_nom" = Eric;
                        "jugador_pat" = Amaro;
                    },
                                    {
                        fechareg = "2014-01-24";
                        "id_jugador" = 5;
                        "jug_playera" = 11;
                        "jug_repre" = N;
                        "jugador_mat" = Cruz;
                        "jugador_nom" = Brayan;
                        "jugador_pat" = Rea;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 12;
                        "jug_playera" = 16;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Paul;
                        "jugador_pat" = x;
                    },
                                    {
                        fechareg = "2014-01-10";
                        "id_jugador" = 1;
                        "jug_playera" = 1;
                        "jug_repre" = N;
                        "jugador_mat" = Soto;
                        "jugador_nom" = Ulises;
                        "jugador_pat" = Ramirez;
                    },
                                    {
                        fechareg = "2014-02-15";
                        "id_jugador" = 8;
                        "jug_playera" = 18;
                        "jug_repre" = N;
                        "jugador_mat" = Rios;
                        "jugador_nom" = "Luis Adrian";
                        "jugador_pat" = Benitez;
                    },
                                    {
                        fechareg = "2014-02-22";
                        "id_jugador" = 15;
                        "jug_playera" = 80;
                        "jug_repre" = N;
                        "jugador_mat" = Hernandez;
                        "jugador_nom" = Javier;
                        "jugador_pat" = Cruz;
                    },
                                    {
                        fechareg = "2014-01-10";
                        "id_jugador" = 4;
                        "jug_playera" = 17;
                        "jug_repre" = N;
                        "jugador_mat" = Resendiz;
                        "jugador_nom" = "Luis Angel";
                        "jugador_pat" = Guizar;
                    },
                                    {
                        fechareg = "2014-01-24";
                        "id_jugador" = 11;
                        "jug_playera" = 12;
                        "jug_repre" = N;
                        "jugador_mat" = Juarez;
                        "jugador_nom" = "Brian Osvaldo";
                        "jugador_pat" = Vazquez;
                    },
                                    {
                        fechareg = "2014-01-24";
                        "id_jugador" = 7;
                        "jug_playera" = 22;
                        "jug_repre" = N;
                        "jugador_mat" = Gutierrez;
                        "jugador_nom" = "Atzin Yaret";
                        "jugador_pat" = "Zu\U00f1iga";
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 14;
                        "jug_playera" = 32;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Yahir;
                        "jugador_pat" = Rios;
                    },
                                    {
                        fechareg = "2014-01-10";
                        "id_jugador" = 3;
                        "jug_playera" = 14;
                        "jug_repre" = N;
                        "jugador_mat" = "G\U00f2mez";
                        "jugador_nom" = Jose;
                        "jugador_pat" = Gutierrez;
                    },
                                    {
                        fechareg = "2014-02-15";
                        "id_jugador" = 10;
                        "jug_playera" = 99;
                        "jug_repre" = N;
                        "jugador_mat" = Dominguez;
                        "jugador_nom" = "Diego Samuel";
                        "jugador_pat" = Colin;
                    }
                );
                "id_equipo" = 3;
                locvis = L;
                "nom_equipo" = Alemania;
            },
                    {
                Jugadores =             (
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 1;
                        "jug_playera" = 6;
                        "jug_repre" = N;
                        "jugador_mat" = Rodriguez;
                        "jugador_nom" = "Jesus Antonio";
                        "jugador_pat" = Prado;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 8;
                        "jug_playera" = 111;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Carlos;
                        "jugador_pat" = Torres;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 15;
                        "jug_playera" = 5;
                        "jug_repre" = N;
                        "jugador_mat" = Cruz;
                        "jugador_nom" = "Angel Rogelio";
                        "jugador_pat" = Vargas;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 4;
                        "jug_playera" = 99;
                        "jug_repre" = N;
                        "jugador_mat" = Orozco;
                        "jugador_nom" = "Jose Miguel";
                        "jugador_pat" = Huerta;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 11;
                        "jug_playera" = 3;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Ivan;
                        "jugador_pat" = Moreno;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 7;
                        "jug_playera" = 12;
                        "jug_repre" = N;
                        "jugador_mat" = esquivel;
                        "jugador_nom" = "Cristofer Carlos";
                        "jugador_pat" = Flores;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 14;
                        "jug_playera" = 7;
                        "jug_repre" = N;
                        "jugador_mat" = Sanchez;
                        "jugador_nom" = "Ra\U00f9l";
                        "jugador_pat" = Guzman;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 3;
                        "jug_playera" = 2;
                        "jug_repre" = N;
                        "jugador_mat" = Vazquez;
                        "jugador_nom" = "Cristian Giovany";
                        "jugador_pat" = Moreno;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 10;
                        "jug_playera" = 442;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Victor;
                        "jugador_pat" = Cruz;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 6;
                        "jug_playera" = 22;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Erick;
                        "jugador_pat" = Ledezma;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 13;
                        "jug_playera" = 23;
                        "jug_repre" = N;
                        "jugador_mat" = Sanchez;
                        "jugador_nom" = "Irvin Uriel";
                        "jugador_pat" = Guzman;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 2;
                        "jug_playera" = 1;
                        "jug_repre" = N;
                        "jugador_mat" = Navarrete;
                        "jugador_nom" = Erick;
                        "jugador_pat" = Hernandez;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 9;
                        "jug_playera" = 123;
                        "jug_repre" = N;
                        "jugador_mat" = Ramirez;
                        "jugador_nom" = "Jose Angel";
                        "jugador_pat" = Gonzalez;
                    },
                                    {
                        fechareg = "2014-02-21";
                        "id_jugador" = 5;
                        "jug_playera" = 11;
                        "jug_repre" = N;
                        "jugador_mat" = Mendoza;
                        "jugador_nom" = "Carlos Alberto";
                        "jugador_pat" = Mendoza;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 12;
                        "jug_playera" = 10;
                        "jug_repre" = N;
                        "jugador_mat" = Bernardino;
                        "jugador_nom" = Bernando;
                        "jugador_pat" = Espidio;
                    }
                );
                "id_equipo" = 4;
                locvis = V;
                "nom_equipo" = Chelsea;
            }
        );
        "cal_default" = 0;
        "cal_estatus" = 0;
        "fecha_partido" = "2014-03-01 10:00:00";
        "id_arbitro" = 0;
        "id_jornada" = 11;
        "id_juego" = 2;
    }
    
    How can parse the next array "Equipos"...
    Code:
     "id_equipo" = 3;
                locvis = L;
                "nom_equipo" = Alemania;
    
    And the the last array "Jugadores"...
    Code:
    {
                        fechareg = "<null>";
                        "id_jugador" = 6;
                        "jug_playera" = 8;
                        "jug_repre" = N;
                        "jugador_mat" = Soto;
                        "jugador_nom" = "Marco Antonio";
                        "jugador_pat" = Ramirez;
                    },
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 13;
                        "jug_playera" = 56;
                        "jug_repre" = N;
                        "jugador_mat" = x;
                        "jugador_nom" = Cesar;
                        "jugador_pat" = " Mendieta";
                    }
    
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    To parse your data, you need to understand your data, both its values and its structure.

    If you don't understand the structure, it will be impossible for you to write code that retrieves the correct values.


    Start by looking at your data. Not in whatever app you used to post your initial screenshot, but using a plain programmer's text editor. Open the JSON in Xcode, for example.

    One feature that most programmer's editors have is brace, bracket, and parenthesis matching. That is, if you double-click on a brace, bracket, or parenthesis character, the editor will find the matching opposite character and either select all the text between the two, or take you to the location of the opposite character. In Xcode, it selects the text between.

    So open your JSON text file, and go to the first item in the array named "Partidos". Double-click one of the opening {'s and notice what is selected. Now scroll down to see where the closing } is. Do the same thing for an opening [. In JSON, [ ] enclose an array, and { } enclose an object.
    http://json.org/


    When JSON is parsed using Cocoa classes, an array is represented by NSArray, and an object is represented by NSDictionary. When these Cocoa objects are printed, as in your "Printing description of partido:", the contents of an NSArray are enclosed in ( ) and the contents of an NSDictionary are enclosed in { }.

    Now take the entire text you posted as "Printing description of partido:" and paste it into a new empty Xcode file. Do the same double-click to select the contents between ( )'s and the contents between { }'s.

    Use this technique to break down and understand the detailed structure of your data.


    Next, look at the first several lines of your data:
    Code:
    Printing description of partido:
    {
        Equipos =     (
                    {
                Jugadores =             (
                                    {
                        fechareg = "<null>";
                        "id_jugador" = 6;
                        "jug_playera" = 8;
                        "jug_repre" = N;
                        "jugador_mat" = Soto;
                        "jugador_nom" = "Marco Antonio";
                        "jugador_pat" = Ramirez;
                    },
    
    What does this tell you?

    The first { tells you that "partido" is an NSDictionary. After that comes the name of an object in the NSDictionary: Equipos. Then comes the value of the Equipos item: a (, which means the start of an array. So the item named Equipos is an array, and Equipos itself is a member of the "partido" dictionary.

    What is the code to access an array named "Equipos" residing in the "partido" NSDictionary?

    First, let's look at the code you posted, which I said was probably wrong:
    Code:
            NSDictionary *partidos = [[torneos objectAtIndex: lint] objectForKey: @"Partidos"];
            for (NSDictionary *partido in partidos)
            {
                NSLog(@"id_jornada %@",partido[@"id_jornada"]);
                NSLog(@"fecha_partido %@",partido[@"fecha_partido"]);
                NSDictionary *equipos = [[COLOR="Red"][torneos objectAtIndex:lint][/COLOR] objectForKey: @"Equipos"];
                for (NSDictionary *equipo in equipos)
    
    We've already established that the array named "Equipos" resides in the "partido" dictionary. What does your code do? It tries to retrieve "Equipos" from the 'torneos' object. That makes no sense. It's looking in the wrong place.

    What object should it be looking in? It should look in the partido object:
    Code:
                NSDictionary *equipos = partido[@"Equipos"];
    
    But wait, what type is the "Equipos" item? It's an array, so declaring the variable 'equipos' as an NSDictionary* is wrong. It should be this:
    Code:
                NSArray *equipos = partido[@"Equipos"];
    
    Notice that I was only able to write this code after breaking down, analyzing, and understanding the structure of the data. Without knowing structure, it would be impossible to write that code.

    By the way, this code:
    Code:
     partido[@"Equipos"];
    
    is logically equivalent to this:
    Code:
    [partidos objectForKey:@"Equipos"];
    
    If you didn't know that, then you should read this documentation:
    https://developer.apple.com/library...ollections/FoundationTypesandCollections.html



    What types of items are actually in the Equipos array? To get an answer, look at the first thing after the (. It's a {, which means the start of an NSDictionary (i.e. a JSON object).

    How many items are in the Equipos array? Use double-click matching of { to find the end of each item, and count how many items you have.

    What does each item in the Equipos array contain? Look at the first character after the item's opening {. It's a member name, "Jugadores". And what type is Jugadores? It's an array, because there's an opening (.


    And so it goes all the way into the contents of the Jugadores array, the members of those items, and their values.

    It this seems complex and prone to errors, then draw a map. That is, write down a diagram of exactly what contains what other items, and label each one's type (array, object, etc.).

    If you don't have an accurate map, you won't know the correct structure of the data, and the code you write will refer to the wrong things at the wrong places.


    If you think you can write all the code for all of this without making any mistakes, then you're wrong.

    Once you have a map of the structure, write one piece of code to retrieve the next essential item, then test it. Print the item it has at each point, and confirm that what's printed is what you expect (check the output against your map). If something doesn't match, fix the code (or fix the map).

    You should also learn to use the debugger. It will help you step through, so you can see what actually happens. For example, if the line I hilited in red actually showed you that 'equipos' was null, then you'd know right away that something was wrong, and you could focus your attention there. The debugger lets you look at details of the actual executing code, and looking at details is how you discover whether they're what's expected or not.
     
  7. joak thread starter macrumors newbie

    Joined:
    Mar 7, 2014

Share This Page