以不同格式提取 JSON 数据

Extract the JSON data in different format

我正在尝试获取数组中的 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"...]

请尝试以下代码 -

 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]]];
    }

试试这个代码

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