2

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;
    }
)
donparalias
  • 1,834
  • 16
  • 37
  • 60

3 Answers3

13

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).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    Because it's better to let Apple & iOS *do the work* of converting an Objective C object to a format that's suitable for JSON. Don't do it via brute force, as things might easily break for you either on the server or the client. – Michael Dautermann Jun 22 '12 at 13:36
  • 1
    Well it is about time, I didn't notice that class before.. Thanks Michael for the very useful info – Paul de Lange Jun 22 '12 at 14:09
  • so the *jsonData variable that i am using is the json file? i dont need the next lines? if i put "dataWithJSONObject" i get an error it doesnt work. So i dont understand what u mean.. what do u mean raw nsdata?? i just want to create a json file with that structure – donparalias Jun 22 '12 at 14:17
  • i mean the *jsonString variable – donparalias Jun 22 '12 at 14:30
  • 1
    The "`jsonList`" array is your JSON object which is what you should be shipping up to your server (provided the object returned by "`JSONObjectWithData`" is not null and your "`e`" `NSError` object should be nil too). What you're seeing when you do an "`NSLog`" on it is the same format that would happen if you tried to output a "`NSArray`". You should be okay. – Michael Dautermann Jun 22 '12 at 14:36
3

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.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • That looks correct to me. edit: But I do recommend building the data out of an NSMutableDictionary rather than a string because it's a bit easier to read and put together (and you don't have to deal with any of that character escaping). You're also less likely to make a mistake. – Dima Jun 22 '12 at 14:16
2

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};
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
AAV
  • 3,785
  • 8
  • 32
  • 59