Neu bei der JSON-API, wie greife ich auf die Werte in Objective-C zu?
Posted: 13 Jan 2025, 10:56
Unten ist mein Code für den Zugriff auf die JSON-API von Edmunds.com. Dies funktioniert perfekt, um auf die Informationen zuzugreifen. Ich habe nur Probleme beim Zugriff auf die Schlüssel-Wert-Paare.
Im NSLog aus dem obigen Code wird Folgendes angezeigt:
Meine Hauptfrage ist, wie ich auf den Namen und den Wert für jedes Array zugreifen kann? Nehmen wir an, ich möchte ein UILabel erstellen, das die Zeichenfolge eines der Werte enthält?
Code: Select all
NSURL *equipmentURL = [NSURL URLWithString: [NSString stringWithFormat:@"https://api.edmunds.com/api/vehicle/v2/styles/%@/equipment?fmt=json&api_key=%@", self.carID, apiKey]];
NSData *jsonData = [NSData dataWithContentsOfURL:equipmentURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.engineArray = [NSMutableArray array];
NSArray *equipmentArray = [dataDictionary objectForKey:@"equipment"];
for (NSDictionary *carInfoDictionary in equipmentArray) {
NSArray *attributes = [carInfoDictionary objectForKey:@"attributes"];
NSLog(@"%@", attributes);
}
Code: Select all
2016-11-03 10:21:26.029 CarWise[25766:1896339] (
{
name = "Engine Immobilizer";
value = "engine immobilizer";
},
{
name = "Power Door Locks";
value = "hands-free entry";
},
{
name = "Anti Theft Alarm System";
value = "remote anti-theft alarm system";
}
)