0
我對ios開發人員非常新,我幾乎一直在努力掙扎。真的非常令人沮喪。如何將json響應放入表格單元格
我已將註釋/ 這裏的代碼崩潰 /指示我的應用程序流停止的位置。我與
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.2.4/iRestaurant/users"]];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
執行請求JSON數據fetchedData
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
//NSLog(@"%@", json);
NSArray* latestLoans = [json objectForKey:@"Users"]; //2
NSEnumerator* enumerator = [latestLoans objectEnumerator];
id element;
while(element = [enumerator nextObject]) {
Result *fetchedResults = [Result init];
/*crashes here*/
fetchedResults.name = [[element objectForKey:@"User"] objectForKey:@"name"];
fetchedResults.email = [[element objectForKey:@"User"] objectForKey:@"name"];
[results addObject:fetchedResults];objectForKey:@"name"]);
}
}
結果是一個自定義的對象,是NSObject的子類。我已經導入了Result.h。我還在當前文件的聲明文件中聲明瞭NSMutableArray *結果。我正在將物體推入結果。對於結果的頭文件看起來像
@interface Result : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, assign) int ID;
@end
我將實施的cellForRowAtIndexPath,推動成果的內容到可重複使用的電池。
我的JSON響應是
{"Users":
[{"User":
{"id":"1",
"name":"Kishor kundan",
"email":"[email protected]",
"password":"asdfasdf",
"fb_id":"1234444"
}
},
{"User":
{"id":"2",
"name":"adsfasdf",
"email":"[email protected]",
"password":"asdfasdf",
"fb_id":"123123"
}
}]
}