通过JSON数据设置UISwitches?

时间:2014-10-12 10:15:44

标签: objective-c json cocoa-touch

我想将一些UISwitch设置为HTTP GET请求引入的值。请求正在运行。但是,由于我对iOS编程还不熟悉,所以我不知道如何从拥有一些JSON数据到像PHP中那样分开它。

这就是我目前所拥有的:

- (void)viewDidLoad
{

    [super viewDidLoad];
    NSLog(@"viewdidload");
    self.responseData = [NSMutableData data];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL     URLWithString:@"http://www.test.api/setings"]];

    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:_xAuthToken forHTTPHeaderField:@"X-Auth-Token"];

    request = [mutableRequest copy];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [switch1 setOn:1 animated:NO];
    [switch2 setOn:1 animated:NO];
    [switch3 setOn:1 animated:NO];
    [switch4 setOn:1 animated:NO];
    [switch5 setOn:1 animated:NO];
    [switch6 setOn:1 animated:NO];
    [switch7 setOn:0 animated:NO];
    [switch8 setOn:1 animated:NO];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

    // show all values
    for(id key in res) {

        id value = [res objectForKey:key];

        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;

        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
    }

    // extract specific value...
    NSArray *results = [res objectForKey:@"results"];

    for (NSDictionary *result in results) {
        NSString *icon = [result objectForKey:@"icon"];
        NSLog(@"icon: %@", icon);
    }

}

- (void)viewDidUnload {
    [super viewDidUnload];
}

这就是它从服务器返回的内容:

{"data":
    {"setting1":"1",
     "setting2":"1",
     "setting3":"0",
     "setting4":"0",
     "setting5":"0",
     "setting6":"0",
     "setting7":"0",
     "setting8":"0"}
}

1 个答案:

答案 0 :(得分:0)

只需将1或0数字字符串转换为bool值,然后使用它设置开关状态

//在类头文件中声明此数组开关

property(strong,nonatomic)NSArray *switches;

并在ViewDidLoad方法

中初始化它
switches=[NSArray arrayWithObjects:switch1,switch2,switch3,switch4,switch5,switch6,switch7,switch8,nil];

//并在你的 - (void)connectionDidFinishLoading:(NSURLConnection *)连接方法

 NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

//从json

中的数据对象获取设置和值的数组
NSDictionary *switchValues=[res objectForKey:@"data"];
    // show all values
NSUInteger *switchIndex=0;

//迭代并将值转换为bool并使用该值设置开关状态

    for(id key in switchValues) {

        id value = [switchValues objectForKey:key];   
        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;
        [[switches objectAtIndex:switchIndex] setOn:[valueAsString boolValue] animated:NO];
        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
        switchIndex++;

    }