Question

i found this framewok , it seem easy to user , http://stig.github.com/json-framework/. In the exemple , he have this json :

{ "resultats":{ "joueurs":[ { "nom":"Jean Martin", "score":10000 }, { "nom":"Pierre Dupond", "score":"9000" }, { "nom":"Alice Bateau", "score":"8500" } ] } }

and he parase it like this :

NSDictionary *json    = [myJSON JSONValue];

//récupération  des résultats
NSDictionary *resultats    = [json objectForKey:@"resultats"];

//récupération du tableau de Jouers
NSArray *listeJoueur    =  [resultats objectForKey:@"joueurs"];

//On parcourt la liste de joueurs
for (NSDictionary *dic in listeJoueur) {

//création d'un objet Joueur
Joueur *joueur = [[Joueur alloc] init];

//renseignement du nom
joueur.nom = [dic objectForKey:@"nom"];

//renseingement du score
joueur.score = [[dic objectForKey:@"score"] intValue];

But me ,i don't have array in my JSON . This is my json :

{"escarrival":"NCE","escdepart":"DJE","estarrival":"08.24","estdepart":"06.27","flynumber":"TU 0286","fstatuts":"Vol atterri","proarrival":"08.25","prodepart":"06.30","realarrived":"----","realdepart":"06.27"}

How can i parase it please ? thankx

Was it helpful?

Solution

You don't need to pars it just extract the objects for the keys.

    NSDictionary *jsonDict    = [myJSON JSONValue];

    //création d'un objet Joueur
    Joueur *joueur = [[Joueur alloc] init];

    //renseignement du nom
    joueur.escarrival = [jsonDict objectForKey:@"escarrival"];

    joueur.escdepart = [jsonDict objectForKey:@"escdepart"];

    joueur.estarrival = [jsonDict objectForKey:@"estarrival"];
    joueur.estdepart = [jsonDict objectForKey:@"estdepart"];
    joueur.flynumber = [jsonDict objectForKey:@"flynumber"];
    joueur.fstatuts = [jsonDict objectForKey:@"fstatuts"];
    joueur.flynumber = [jsonDict objectForKey:@"flynumber"];
    joueur.proarrival = [jsonDict objectForKey:@"proarrival"];
    joueur.prodepart = [jsonDict objectForKey:@"prodepart"];
    joueur.realarrived = [jsonDict objectForKey:@"realarrived"];
    joueur.realdepart = [jsonDict objectForKey:@"realdepart"];

Edit:

For better understanding of NSDictionary read Collections Programming Topics from Apple

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top