0

我是这个 Objective-c 编程的新手。我的问题是我需要创建一个名为“X-FBR-App”的标头,并且该标头的内容是 JSON 字符串。JSON 字符串内容为 5 个参数。参数如下所示:

NSString *nid = @":";
    NSString *vocab = @":";
    NSString *inturl = @"testoverview";
    NSString *mail = @"your@fbr.dk";
    NSString *md5pw = @"password";

我使用 iOS5 和内置的 JSON 库。关于如何做到这一点的任何想法?

4

1 回答 1

1

像这样的东西......

NSString *nid = @":";
    NSString *vocab = @":";
    NSString *inturl = @"testoverview";
    NSString *mail = @"your@fbr.dk";
    NSString *md5pw = @"password";

NSArray *jsonArray = [NSArray arrayWithObjects:inturl, mail, md5pw, nil]; // create your json array or dict

NSError *error; 

// serialize data
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray 
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    // get json string
    NSString *jsonString = [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] autorelease];

    // do a Request

    NSString *url = @"your url";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:30.0];


    [request setValue:jsonString forHTTPHeaderField:@"Field You Want To Set"];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
于 2012-04-13T10:19:35.300 回答