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
}
Ah das funktioniert perfekt !! Vielen Dank :) – domshyra
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
Dann solltest du so etwas tun:' self.lat.text = [[[information valueForKey: @ "Ort"] objectAtIndex: 0] valueForKey : @ "Latitude"]; ' – Alladinian