以不同格式提取JSON数据

时间:2015-05-25 08:52:17

标签: ios objective-c json nsdata

我试图在数组中获取JSON响应,但我只想要没有索引的数据。这是我的代码:

NSString *myRequestString = [NSString stringWithFormat:@"getSetAvailability=yes&userId=41"];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://Crux.solutions:giggy@182.184.71.153:81/Giggy/getUser.php"]];
// 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);



NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSArray *Sunday=[dict objectForKey:@"Sunday"];
NSLog(@"Sunday: %@",[Sunday description]);

获得这样的回复:

{"Sunday": {"0": "1 AM", "1": "3 AM", "2": "2 AM", "3": "12 AM", "4": "5 AM", "5": "4 AM", "6": "6 AM"}, "Monday": {"0": "1 AM", "1": "3 AM", "2": "2 AM", "3": "12 AM", "4": "5 AM"}, "Tuesday": {"0": "1 AM", "1": "3 AM", "2": "2 AM", "3": "12 AM", "4": "5 AM"}, "Wednesday": {"0": "1 AM", "1": "3 AM", "2": "2 AM", "3": "12 AM", "4": "5 AM"}, "Thursday": {"0": "1 AM", "1": "3 AM", "2": "2 AM", "3": "12 AM", "4": "5 AM"}, "Friday": {}, "Saturday": {}}

特别是周日阵列是这样的:

Sunday: {
0 = "1 AM";
1 = "3 AM";
2 = "2 AM";
3 = "12 AM";
4 = "5 AM";
5 = "4 AM";
6 = "6 AM";
}

我希望我的数组像[@"1 AM", @"3AM",@"2AM"...]

2 个答案:

答案 0 :(得分:2)

试试此代码

NSArray *Sunday=[[dict objectForKey:@"Sunday"] allValues];
NSLog(@"Sunday: %@",[Sunday description]);

答案 1 :(得分:1)

请尝试以下代码 -

 NSMutableArray *arrayValue =[[NSMutableArray alloc] init];

    NSArray *Sunday=[dict objectForKey:@"Sunday"];

    for (NSDictionary *dictValue in Sunday)
    {
        NSLog(@"%@ value %@",[[dictValue allKeys] objectAtIndex:0],[dictValue valueForKey:[[dictValue allKeys] objectAtIndex:0]]);

        [arrayValue addObject:[dictValue valueForKey:[[dictValue allKeys] objectAtIndex:0]]];
    }