Reputation: 1834
Ok i need to use json on my iOS application to send/receive data to/from my server. The problem is i got a bit confused about how the json file should look like. I need the json file that i am sending to the server to look like this :
NSString *jsonString = @"{\"eventData\":{\"eventDate\":\"Jun 13, 2012 12:00:00 AM\",\"eventLocation\":{\"latitude\":43.93838383,\"longitude\":-3.46},\"text\":\"lalalala\",\"imageData\":\"raw data\",\"imageFormat\":\"JPEG\",\"expirationTime\":1339538400000},\"type\":\"ELDIARIOMONTANES\",\"title\":\"accIDENTE\"}";
When i try to print this in the console i get :
jsonString: {"eventData":{"eventDate":"Jun 13, 2012 12:00:00 AM","eventLocation":{"latitude":43.93838383,"longitude":-3.46},"text":"lalalala","imageData":"raw data","imageFormat":"JPEG","expirationTime":1339538400000},"type":"ELDIARIOMONTANES","title":"accIDENTE"}
which according to online json validators seems to be a valid json format.
HOWEVER , after searching the web , and of course stackoverflow , and after have asked another question about this , i found out that i should convert the above NSString to a json file like this :
NSString *jsonString = @"{\"eventData\":{\"eventDate\":\"Jun 13, 2012 12:00:00 AM\",\"eventLocation\":{\"latitude\":43.93838383,\"longitude\":-3.46},\"text\":\"lalalala\",\"imageData\":\"raw data\",\"imageFormat\":\"JPEG\",\"expirationTime\":1339538400000},\"type\":\"ELDIARIOMONTANES\",\"title\":\"accIDENTE\"}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
But if i try to print the jsonList variable , which i thought it would be the json format/file i get this :
jsonList: {
eventData = {
eventDate = "Jun 13, 2012 12:00:00 AM";
eventLocation = {
latitude = "43.93838383";
longitude = "-3.46";
};
expirationTime = 1339538400000;
imageData = "raw data";
imageFormat = JPEG;
text = lalalala;
};
title = accIDENTE;
type = ELDIARIOMONTANES;
}
which is not a valid json file!!!! So my question is , if i had from the very beginning the correct format to my json file why all this extra code to "convert" it to json?? I guess , i MUST be missing something here or misunderstood the functionality of the code. Thank you very much for reading my post :)
Upvotes: 0
Views: 252
Reputation: 82267
I think you're getting yourself tied up in knots there a little bit :-) If you have a NSString containing exactly the data you wish to send, there's no point using JSONObjectWithData et al, you might as well just send the string you have already.
However, if you want to extract bits of data you're receiving as JSON from the server, that's when you'd create want to use those methods. It's much easier and more reliable to get data out of a parsed JSON object that just a big old plain JSON string (regex'ing is not a good way to extract data from JSON).
The other case is where you want to build up a large and complex JSON object in code, and then send that as a JSON string to your server. You don't want to try this by appending NSStrings together (gets nasty quickly) it's better to put your data is NSMutableArrays or NSDictionaries and then convert to JSON.
Here's a quick example of this ( from here )
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:@"[email protected]", @"user",@"mypass", @"pass", nil];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsonString);
Upvotes: 1
Reputation: 31304
What you're seeing is the correct behaviour:
if i try to print the jsonList variable , which i thought it would be the json format/file
Your jsonList
variable is a NSMutableArray
. The -JSONObjectWithData
method returns an Objective-C object (a Core Foundation object) - not a JSON string. It has parsed your JSON string, and returned an object that represents that JSON data.
Upvotes: 1