How to correctly convert an NSDictionary to a json format , in iOS?

donparalias picture donparalias · Jun 26, 2012 · Viewed 28.4k times · Source

I am trying to send to my server a json file. The json format should look exactly like this:

{
    "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"
}

I hardcoded this and worked like this:

NSString *jsonString = @"{\"eventData\":{\"eventDate\":\"Jun 13, 2012 12:00:00 AM\",\"eventLocation\":{\"latitude\":43.93838383,\"longitude\":-3.46},\"text\":\"hkhkjh\",\"imageData\":\"\",\"imageFormat\":\"JPEG\",\"expirationTime\":1339538400000},\"type\":\"Culture\",\"title\":\"accIDENTE\"}";

So i want to do the exact same thing but using an NSDictionary cause of course i dont want to hardcode all my data. So i am creating an NSDictionary like this:

NSMutableDictionary *eventLocation = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"43.93838383",@"latitude",@"-3.46", @"longitude" , nil];

    NSMutableDictionary *eventData = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventLocation,@"eventLocation", nil];
                        [eventData setObject:@"Jun 13, 2012 12:00:00 AM" forKey:@"eventDate"];
                        [eventData setObject:@"hjhj" forKey:@"text"];
                        [eventData setObject:@"raw data" forKey:@"imageData"];
                        [eventData setObject:@"JPEG" forKey:@"imageFormat"];
                        [eventData setObject:@"1339538400000" forKey:@"expirationTime"];

    NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,@"eventData", nil];
                        [finalDictionary setObject:@"ELDIARIOMONTANES" forKey:@"type"];
                        [finalDictionary setObject:@"accIDENTE" forKey:@"title"];

Now i have to convert this to the correct json format , so that i can send it to my server. I tried using this :

NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalDictionary 
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error]

but now when i print the jsonData i get this :

jsonData: <7b0a2020 22657665 6e744461 74612220 3a207b0a 20202020 22696d61 6765466f 726d6174 22203a20 224a5045 47222c0a 20202020 22746578 7422203a 2022686a 686a222c 0a202020 2022696d 61676544 61746122 203a2022 72617720 64617461 222c0a20 20202022 65787069 72617469 6f6e5469 6d652220 3a202231 33333935 33383430 30303030 222c0a20 20202022 6576656e 744c6f63 6174696f 6e22203a 207b0a20 20202020 20226c6f 6e676974 75646522 203a2022 2d332e34 36222c0a 20202020 2020226c 61746974 75646522 203a2022 34332e39 33383338 33383322 0a202020 207d2c0a 20202020 22657665 6e744461 74652220 3a20224a 756e2031 332c2032 30313220 31323a30 303a3030 20414d22 0a20207d 2c0a2020 22747970 6522203a 2022454c 44494152 494f4d4f 4e54414e 4553222c 0a202022 7469746c 6522203a 20226163 63494445 4e544522 0a7d>

What is this exactly?? How can i just convert that dictionary to look exactly like this:

{
    "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"
}

If possible i want the above format to be an NSString cause that was what i was sending to the server and it was working. I mean an NSString , in json format like this. Thank you very much for bothering reading all this! :D

Answer

Antonio MG picture Antonio MG · Jun 26, 2012

Your conversion is fine, NSData looks like that because it's a bunch of bytes!

You need to convert it to string, try this:

NSString* aStr;
aStr = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];

The method:

+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error

Returns a NSData object, NSData is just an object wrapper for a byte representation, so your dictionary is there, but in the form of a byte representation.

Then you use:

- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

This method takes your byte representation and turn it into a string, using the encoding that you provide.