2014-11-21 15 views
8

Ich möchte NSDictionary zu JSON String konvertieren. Alles funktioniert gut, ich habe ein kleines Problem, das wie folgt beschrieben wird: Ich habe einen folgenden Code für die Konvertierung von NSDictionary zu NSString:Problem beim Konvertieren von NSDictionary in JSON String, ersetzen/mit /

-(NSString *)dictToJson:(NSDictionary *)dict 
{ 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; 
    return [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; 
} 

ich rufe die Methode als:

NSLog(@"%@", [self dictToJson:@{@"hello" : @"21/11/2014 10:07:42 AM"}]); 

nach dem Ausgang dieses NSLog ist:

{ 
    "hello" : "21\/11\/2014 10:07:42 AM" 
} 

ich folgende Ausgabe erwarten, wie kann ich es erreichen:

{ 
     "hello" : "21/11/2014 10:07:42 AM" 
} 

es durch die Verwendung stringByReplacingOccurrencesOfString Verfahren durchgeführt werden kann, aber ich will nicht, diese verwenden. Gibt es einen anderen Weg, um das Gleiche zu erreichen?

+0

ist etwas nicht in Ordnung, wenn Sie die 'description' Methode für das Wörterbuch einfach anrufen? –

Antwort

1

Beim Konvertieren des JSON-Objekts in String wird der Schrägstrich verlassen. Deshalb wird der Back Slash in Ihrem Ergebnis hinzugefügt.

Wenn Sie die Zeichenfolge zurück in JSON-Objekt konvertieren und das Objekt protokolliert haben, können Sie das Ergebnis wie erwartet sehen. So können Sie überprüfen, dass mit der Zeichenfolge nichts falsch ist.

0

fügen Sie diese

-(NSString*) bv_jsonStringWithPrettyPrint:(BOOL) prettyPrint; 



-(NSString *)dictToJson:(NSDictionary *)dict 
    { 

     NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dict 
                options:(NSJSONWritingOptions) (prettyPrint ? NSJSONWritingPrettyPrinted : 0) 
                error:&error]; 

    if (! jsonData) { 
     NSLog(@"bv_jsonStringWithPrettyPrint: error: %@", error.localizedDescription); 
     return @"{}"; 
    } else { 
     return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    } 

    } 

beziehen sich diese Generate JSON string from NSDictionary in iOS

2

die Sie interessieren,

NSData *json = [NSJSONSerialization dataWithJSONObject:dict 
               options:0 
               error:&error]; 
NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; 
// This will be the json string in the preferred format 
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; 

// And this will be the json data object 
NSData *processedData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
21

NSDictionary - to - String

NSError * err; 
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:response options:0 error:&err]; 
NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

string - to - NSDictionary

NSError * err; 
NSDictionary * response = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:[NSData dataFromString:str] options:NSJSONReadingMutableContainers error:&err]; 
Verwandte Themen