Monday 21 April 2014

Convert NSDictionary to NSString.

Sometime we need to convert the dictionary(received from server or database)
to the JSON string. We can use this simple function.


+ (NSString*) convertDictionaryToString:(NSMutableDictionary*) dict
{
    NSError* error;
    NSDictionary* tempDict = [dict copy]; // get Dictionary from mutable Dictionary

    //giving error as it takes dic, array,etc only. not custom object.
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:tempDict
                                                      options:NSJSONReadingMutableLeaves error:&error];
    NSString* nsJson=  [[NSString allocinitWithData:jsonData
                                             encoding:NSUTF8StringEncoding];
    return nsJson;
}

No comments:

Post a Comment