20
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

objective-cでユニコードエスケープされたJSON文字列をデコードする方法(not Parse)

Last updated at Posted at 2013-12-20

ユニコードエスケープされたJSON文字列をデコードする方法が見つからないので調べてみました。
(パースする方法はいくらでもあるのですが。。。)
こんな感じでUtilityクラスにでも定義して呼び出すと便利な予感です。

Utils.h
+ (NSString *)decodeJSONString:(NSString *)input;
Utils.m
+ (NSString *)decodeJSONString:(NSString *)input
{

    NSString *esc1 = [input stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
    NSData *data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
    NSString *unesc = [NSPropertyListSerialization propertyListFromData:data
                                                       mutabilityOption:NSPropertyListImmutable format:NULL
                                                       errorDescription:NULL];
    assert([unesc isKindOfClass:[NSString class]]);
    return unesc;
}

使い方
NSString *escapedString = [Utils decodeJSONString:error];

下記投稿のコピペですが(白目)
http://stackoverflow.com/questions/2099349/using-objective-c-cocoa-to-unescape-unicode-characters-ie-u1234

日本語でこそ必要そうな情報なので投稿してみます。

20
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?