2016-08-02 21 views
-1

ich eine API bin mit JSON-Daten in Objektdaten, besuchen Sie bitte die API verwende ich Please Visit the API page which I am usingErste Fehler beim JSON-Daten konvertieren Objekt in iOS

Hier ist die Schnapp meines Codes mit markierten Fragen zu konvertieren Issues

und hier ist der Rohcode

-(void) retrieveData{ 
    NSURL * url = [NSURLURLWithString:getDataUrl]; 
NSData * data = [NSData dataWithContentsOfURL:url]; 
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil]; 

NSLog(@"JsonArray %@", jsonArray); 

//setup yougaArray 
yougaArray = [[NSMutableArray alloc] init]; 

//Loop through our jsonArray 

for (int i = 0; i<jsonArray.count; i++) 
{ 
    NSString * yId = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"id"]; 
    // NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"]; 
    NSString * yName = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"name"]; 
    NSString * yDescription = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"description"]; 
    NSString * yImage = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"image"]; 

    //Add the city object to our citiesArray 

    [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; 

    } 
[self.tableView reloadData]; 

    } 
+0

'objectAtIndex: @" Kategorien "' => Warum setzt man einen NSString dort? Außerdem müssen Sie die Struktur von JSON verstehen, bevor Sie versuchen, es zu analysieren. – Larme

+0

können Sie 'NSLog (@" JsonArray% @ ", jsonArray);'] –

+0

können Sie übergeben den Ganzzahlwert in der [arrayName objectAtIndex: 0] wie dieser Zugriff auf das Array –

Antwort

2

Dank @Lame folgte ich Ihren Code, wie es mir 6 bis 8 Fehler gab, aber ich verstehe Ihren Code & konfigurierten Fehler, dass hier nun die komplette Lösung, die nach meinen Anforderungen arbeitet oder (perfekte Antwort nach zu meiner Frage)

-(void) retrieveData{ 
    NSURL * url = [NSURL URLWithString:getDataUrl]; 
    NSData * data = [NSData dataWithContentsOfURL:url]; 
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

    NSDictionary *dataJSON = [jsonDict objectForKey:@"data"]; 
    NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"]; 

    yougaArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < allCategoriesJSON.count; i ++) 
    { 
     NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i]; 
     NSString *yId = [aCategoryJSON objectForKey:@"id"]; 
     NSString *yName = [aCategoryJSON objectForKey:@"name"]; 
     NSString *yDescription = [aCategoryJSON objectForKey:@"description"]; 
     NSString *yImage = [aCategoryJSON objectForKey:@"image"]; 
     [yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; 
    } 
    [self.tableView reloadData]; 
2

Nach Ihrer JSON-Antwort sein ein Dictionary Typ Objekt nicht ein array so sollte der Code wie folgt sein,

NSMutableDictionary *dictData = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil]; 

    NSLog(@"JsonArray %@", dictData); 

     NSArray *jsonArray=[[dictData objectForKey:@"data"] objectForKey:@"categories"]; 
    //setup yougaArray 
    yougaArray = [[NSMutableArray alloc] init]; 


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


     NSString * yId = [[jsonArray objectAtIndex:i] objectForKey:@"id"]; 
     // NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"]; 
     NSString * yName = [[jsonArray objectAtIndex:i] objectForKey:@"name"]; 
     NSString * yDescription = [[jsonArray objectAtIndex:i] objectForKey:@"description"]; 
     NSString * yImage = [[jsonArray objectAtIndex:i] objectForKey:@"image"]; 

     //Add the city object to our citiesArray 

     [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; 

    } 
    [self.tableView reloadData]; 

Hoffe, dass es für Sie arbeitet. Gib mir Bescheid!! Glückliche Codierung. :)

+0

Danke für den Beitrag @Ravi, ich habe die Lösung für die gestellte Frage hochgeladen, bitte überprüfen Sie das. –

0

Dies ist die JSON:

{ 
    "meta": { 
     "status": "200", 
     "msg": "OK" 
    }, 
    "data": { 
     "total_pages": 0, 
     "total_categories": 2, 
     "current_page": 1, 
     "next_page": 0, 
     "categories": [{ 
      "id": "2", 
      "name": "Articles", 
      "description": "Yoga Articles", 
      "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg" 
     }, { 
      "id": "1", 
      "name": "Poses", 
      "description": "Yoga Poses", 
      "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg" 
     }] 
    } 
} 

Ihre JSON ist ein NSDictionary auf höchstem Niveau! Nicht ein NSArray! Vermeiden Sie auch alle objectForKey:/objectAtIndex: in der gleichen Zeile/Anweisung, es ist schwerer zu lesen, aber auch schwieriger zu debuggen, vor allem, wenn Sie nicht wissen, was Sie tun. Wenn es einen Fehlerparameter gibt, verwenden Sie ihn auch, geben Sie nil nicht ein.

So:

NSError *errorJSON = nil; 
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorJSON]; 
if (errorJSON) 
{ 
    NSLog(@"ErrorJSON: %@", errorJSON); 
    return; 
} 
NSDictionary *dataJSON = [jsonDict objectForKey:@"data"]; 
NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"]; 
for (NSUIInteger i = 0; i < allCategoriesJSON.count; i ++) 
{ 
    NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i]; 
    NSString yID = [aCategoryJSON objectForKey:@"id"]; 
    NSString yName = [aCategoryJSON objectForKey:@"name"]; 
    NSString yDescription = [aCategoryJSON objectForKey:@"description"]; 
    NSString yImage = [aCategoryJSON objectForKey:@"image"]; 
    [yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; 
} 
+0

@Lame Danke für die Hilfe, zuerst habe ich oben den Code von "Monika Patel" befolgt, der hilfreich war, aber mir keine passende Lösung zum Laden von Daten in meine benötigten Objekte gab. Ich mache geringfügige geeignete Änderungen, die mir meine erforderliche Lösung nicht geben konnten bereit, Daten in meine erstellten Objekte wie yId, yName, yDescription, yImage, zu setzen, aber wenn ich Ihren Code verwendete, gab es wenige Fehler, die ich konfigurierte, und jetzt gibt es mir meine erforderliche Lösung. Warten Sie, bis ich eine Antwort in den Antwortbereich gebe, . Daher ist Ihr vorgeschlagener Code die richtige Lösung. –

+0

Ich habe meinen Code mit einem Compiler nicht herausgefordert. Ich habe es gerade hier geschrieben. Es kann kleine Probleme mit der Syntax geben. Was waren die Fehler? – Larme

+0

Ich habe die korrigierte Version Ihres Codes hochgeladen. Bitte überprüfen Sie, dass Ihre Arbeit wirklich sehr hilfreich ist. –

1

Sie JSON scheint wie folgt aus:

{ 
"meta": { 
    "status": "200", 
    "msg": "OK" 
}, 
"data": { 
    "total_pages": 0, 
    "total_categories": 2, 
    "current_page": 1, 
    "next_page": 0, 
    "categories": [{ 
     "id": "2", 
     "name": "Articles", 
     "description": "Yoga Articles", 
     "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg" 
    }, { 
     "id": "1", 
     "name": "Poses", 
     "description": "Yoga Poses", 
     "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg" 
    }] 
} 
} 

jetzt Ihre vorhandenen Code durch den folgenden Code ersetzen:

NSData * data = [NSData dataWithContentsOfURL:url]; 
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil]; 

NSLog(@"JsonArray %@", jsonArray); 

//setup yougaArray 
yougaArray = [[NSMutableArray alloc] init]; 

//Loop through our jsonArray 

NSArray *dataArray = [[jsonArray [email protected]"data"] objectForKey:@"categories"]; 

for (int i = 0; i < dataArray.count; i++) { 
    NSString * yId = [[dataArray objectAtIndex:i] objectForKey:@"id"]; 
    NSString * yName = [[dataArray objectAtIndex:i] objectForKey:@"name"]; 
    NSString * yDescription = [[dataArray objectAtIndex:i] objectForKey:@"description"]; 
    NSString * yImage = [[dataArray objectAtIndex:i] objectForKey:@"image"]; 

    //Add the city object to our citiesArray 

    [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; 
} 

[self.tableView reloadData]; 

Lassen Sie mich wissen, ob die Lösung funktioniert für dich, auch wenn etwas auftaucht.

+0

Dank @Siddharth Sunil für Ihre Zeit, Aufmerksamkeit & Beitrag, ich habe die Lösung für die gestellte Frage hochgeladen. was von einer anderen Person "Lame" mit einigen Fehlern gegeben ist, die ich aufgelöst habe. –