Reputation: 1834
I have some data and need to create a json file with this structure :
{"eventData":{"eventDate":"Jun 13, 2012 12:00:00 AM","eventLocation":{"latitude":43.93838383,"longitude":-3.46},"text":"hjhj","imageData":"raw data","imageFormat":"JPEG","expirationTime":1339538400000},"type":"ELDIARIOMONTANES","title":"accIDENTE"}
Can Please someone give me some code how to to put my data in a json file with this structure or at least an example on how u make json structures?
EDIT
Ok so i wrote some code :
NSString *jsonString = @"[{\"eventData\":{\"eventDate\":\"Jun 13, 2012 12:00:00 AM\",\"eventLocation\":{\"latitude\":43.93838383,\"longitude\":-3.46},\"text\":\"hjhj\",\"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];
NSLog(@"jsonList: %@", jsonList);
jsonList is a json file now ??? And is it in the correct format?? Cause when i print it the output is :
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 = hjhj;
};
title = accIDENTE;
type = ELDIARIOMONTANES;
}
)
Upvotes: 2
Views: 10952
Reputation: 23624
If you only need to support iOS5 and higher, you can use Apple's NSJSONSerialization as Michael said. For earlier iOS versions, you will need to use a third party json parsing library (I recommend JSONKit, it's the fastest).
Ultimately what you're going to be doing is creating an NSDictionary with the key/value structure you want your JSON data to have, and using one of these methods to convert it.
Upvotes: 2
Reputation: 3803
Edited the answer:
Please use NSJSONSerialization to create a JSON object. To create JSON object please don't use string. Use something like dictionaryWithObjectsAndKeys:
and the convert it into JSON object
NSDictionary *PostParams = [NSDictionary dictionaryWithObjectsAndKeys:
[value], [key], nil];
If you are using iOS 6 and Xcode 4.3 then
NSDictionary *PostParams = @{value, key};
Upvotes: 2
Reputation: 89509
Have you looked at Apple's "NSJSONSerialization
" class yet? It became available as of iOS 5.0.
You can use it to convert an Objective C object into a JSON data stream that you can ship up to your web service.
If you want to support OS's older than 5.0, there are other possibilities available (take a look at this related question).
Upvotes: 12