【发布时间】:2015-07-10 08:03:59
【问题描述】:
这是包含多个对象的 JSON 数据,我只想解析“continueWatching”对象,所以我只是分享了它:
{
"message": {
"continueWatching": [{
"id": "2",
"video": [{
"videoName": "fsfdsf",
"coverPicture": ""
}
],
"cursorPosition": "12:32:25",
"dateWatched": "2015-07-08 00:00:00",
"updated": "2015-07-15 12:12:34"
}, {
"id": "1",
"video": [{
"videoName": "Hello",
"coverPicture": ""
}
],
"cursorPosition": "15:42:41",
"dateWatched": "2015-07-02 00:00:00",
"updated": "2015-07-02 00:00:00"
}, {
"id": "3",
"video": [{
"videoName": "adfafadf",
"coverPicture": ""
}
],
"cursorPosition": "12:32:25",
"dateWatched": "2015-07-01 00:00:00",
"updated": "2015-07-02 00:00:00"
}]
}
我的代码现在,我打印了整个 JSON 数据和消息对象,我需要将所有“videoName”放在一个按其“id”数字排列的数组中,而且我还希望将所有“cursorPosition”放在一个按“id”排列的单个数组
NSString *myRequestString = [NSString stringWithFormat:@"getRecommendations=all&profileId=1"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",response);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
BOOL boolean=[[dict objectForKey:@"boolean"]boolValue];
NSArray *message=[dict objectForKey:@"message"];
NSLog(@"kajhsdahfohofhoaehfpihjfpejwfp%@",message);
【问题讨论】:
-
看来您正朝着正确的方向前进。有什么问题?
-
NSDictionary* continueWatching = [[dict objectForKey:@"message"] objectForKey:@"continueWatching"]; --OR-- NSArray* continueWatching = [[dict objectForKey:@"message"] objectForKey:@"continueWatching"];
-
它应该是
NSArray* continueWatching = [[dict objectForKey:@"message"] objectForKey:@"continueWatching"];,因为有一个数组针对键“continueWatching”。 -
我正在尝试:NSArray* continueWatching = [[dict objectForKey:@"message"] objectForKey:@"continueWatching"]; for(NSArray *tmpArr1 in continueWatching){ [title addObject:[tmpArr1 valueForKey:@"cursorPosition"]]; NSLog(@"oiajsdohioqsd5 %@",[tmpArr1 valueForKey:@"cursorPosition"]);}
-
@SamraanKhaan 这就是我在数组中获取 cursorPositions 的方式,现在正在处理 videoNames
标签: ios objective-c arrays json parsing