我的任务涉及在从 Web 服务获得响应之前为 JSON 字符串创建正确的设计。JSON 字符串必须如下所示:
{"nid":"","vocab":"", "inturl":"testoverview", "mail":"", "md5pw":""}
我的 JSON 字符串如下所示:
"nid:",
"vocab:",
"inturl:testoverview",
"mail:",
"md5pw:"
如您所见,它的构建方式不同,我没有使用大括号,也没有以正确的方式分隔字符串。我不知道该怎么做。
我的代码在这里:
NSString *nid = @"nid:";
NSString *vocab = @"vocab:";
NSString *inturl = @"inturl:testoverview";
NSString *mail = @"mail:";
NSString *md5pw = @"md5pw:";
NSArray *jsonArray = [NSArray arrayWithObjects:nid, vocab, inturl, mail, md5pw, nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
FSProductTestHandler *handler = [[FSProductTestHandler alloc] init];
if (!jsonData) {
NSLog(@"Got an error; %@", error);
} else if(jsonData) {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *url = @"http://www.taenk.dk/services/mobile";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];
[request setValue:jsonString forHTTPHeaderField:@"X-FBR-App"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
谁能帮我解决这个问题?