2012-06-02 4 views
17

Ich versuche, Daten zu Etiketten aus meiner JSON-Datei auf einem einfachen ViewController übergeben, aber ich weiß nicht, wo diese Daten tatsächlich übergeben werden. Könnte ich einfach zu meiner setDataToJson Methode hinzufügen oder würde ich die Daten in meiner viewDidLoad Methode hinzufügen?Übergeben von Daten aus der lokalen Datei mit JSON

hier ist mein Code

@interface NSDictionary(JSONCategories) 
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation; 
@end 

@implementation NSDictionary(JSONCategories) 

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{ 
    NSData* data = [NSData dataWithContentsOfFile:fileLocation]; 
    __autoreleasing NSError* error = nil; 
    id result = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions error:&error]; 
    if (error != nil) return nil; 
    return result; 
} 
@end 

@implementation ViewController 
@synthesize name; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 

-(void)setDataToJson{ 

    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"]; 
    name.text = [infomation objectForKey:@"AnimalName"];//does not pass data 
} 

Antwort

40

Das Problem ist die Art und Weise Sie versuchen, Ihre Datei abzurufen. Um es richtig zu machen, solltest du zuerst seinen Pfad im Bündel finden. Probieren Sie etwas wie folgt aus:

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileLocation stringByDeletingPathExtension] ofType:[fileLocation pathExtension]]; 
    NSData* data = [NSData dataWithContentsOfFile:filePath]; 
    __autoreleasing NSError* error = nil; 
    id result = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions error:&error]; 
    // Be careful here. You add this as a category to NSDictionary 
    // but you get an id back, which means that result 
    // might be an NSArray as well! 
    if (error != nil) return nil; 
    return result; 
} 

Danach tun und einmal Ihre Ansicht geladen wird, sollten Sie in der Lage sein, Ihre Etiketten zu setzen, indem die json wie diese Abrufen:

-(void)setDataToJson{ 
    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"]; 
    self.name.text = [infomation objectForKey:@"AnimalName"]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setDataToJson]; 
} 
+0

Ah das funktioniert perfekt !! Vielen Dank :) – domshyra

+0

Würde ich meine nsdictionary Funktion ändern, wenn ich Informationen aus einem Array von Daten in der JSON-Datei erhalten wollte 'self.lat.text = [[infomation valueForKey: @" Location "] valueForKey: @" Latitude "];' das ist, was ich habe, und das ist, was die JSON wie sehen '" Location ": [ { "Longitude": 0, "Latitude": 0 } ],' und als ich führe es aus es sagt '- [__ NSArrayI isEqualToString:]: unerkannter Selektor an Instanz gesendet ' – domshyra

+0

Dann solltest du so etwas tun:' self.lat.text = [[[information valueForKey: @ "Ort"] objectAtIndex: 0] valueForKey : @ "Latitude"]; ' – Alladinian

1

Es valueForKey stattdessen sein sollte.

Beispiel:

name.text = [infomation valueForKey:@"AnimalName"]; 
Verwandte Themen