I have task that involves creating the right design for the JSON string before I can get a response from the webservice. The JSON string has to look like this:
{"nid":"","vocab":"", "inturl":"testoverview", "mail":"", "md5pw":""}
and my JSON string looks like this:
"nid:",
"vocab:",
"inturl:testoverview",
"mail:",
"md5pw:"
as you can see it’s not built the same way, I’m not using braces, or separating the strings the right way. And I don’t know how to do this.
my code for this is here:
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];
}
Can anyone help me with this issue?
You are feeding an array to the serialisation which means you’ll get a JSON array as output i.e soemthing like:
(note the brackets
[ ...]instead of braces{ ... })You need to build an NSDictionary and for your particular example, the quickest way is like this:
Feed that into
NSJSONSerializationand you’ll get what you want.