我收到從我的服務器具有以下格式的JSON array
:ios通過JSON循環?
{ messageArray:
[ { msgFrom: 'V351315826',
msgDate: Tue Oct 29 2013 04:00:35 GMT+0000 (UTC),
msgBody: 'Hi?',
channelID: 'V351315826' },
{ msgFrom: 'V351315826',
msgDate: Tue Oct 29 2013 04:00:38 GMT+0000 (UTC),
msgBody: 'Hello!',
channelID: 'V351315826' } ] }
我怎麼可以遍歷呢?
到目前爲止什麼是得到的是:
NSError* error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [(NSString *) message dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers
error: &error];
NSArray *messageArray = [JSON objectForKey:@"messageArray"];
// Parse and loop through the JSON
for (NSDictionary * dataDict in messageArray) {
NSString * messageID = [dataDict objectForKey:@"id"];
NSString * msgFrom = [dataDict objectForKey:@"msgFrom"];
NSString * msgTime = [dataDict objectForKey:@"msgDate"];
NSLog(@"FROM ---> %@", dataDict);
}
什麼問題?你想做什麼? – BergQuester
我想遍歷messageArray中的每個msgFrom,msgDate,channelID – Alosyius
是否有問題?你所擁有的代碼看起來是一個好的開始。 – BergQuester