iOSのNSDictionaryからJSON文字列を生成する 質問する

iOSのNSDictionaryからJSON文字列を生成する 質問する

があり、を使用してdictionaryを生成する必要があります。 変換することは可能ですか? 皆さん、これについて助けていただけませんか?JSON stringdictionary

ベストアンサー1

AppleはiOS 5.0とMac OS X 10.7でJSONパーサーとシリアライザーを追加しました。NSJSONシリアル化

NSDictionary または NSArray から JSON 文字列を生成するために、サードパーティのフレームワークをインポートする必要がなくなりました。

やり方は次のとおりです:

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToOutput 
                                                   options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

おすすめ記事