iOS で JSON 形式データを作成する方法

json-framework → SBJson
TouchJSON
などが有名のようである。(2011/09時点)

ー SBJson を使用して、JSON 形式データを出力する
値に数値を指定したい場合、NSNumber をセットすればよい。

1
2
3
4
5
6
7
8
9
10
11
12
NSDictionary *dict0, *dict1;
dict0 = [NSDictionary dictionaryWithObjectsAndKeys:@"value00", @"key00", 
                                                   @"value01", @"key01", nil];
dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value10", @"key10", 
                                                   @"value11", @"key11", nil];
NSArray *array = [[NSArray alloc] initWithObjects:dict0, dict1, nil];
 
SBJsonWriter *writer = [[[SBJsonWriter alloc] init] autorelease];
writer.humanReadable = YES;
writer.sortKeys = YES;
 
NSLog(@"%@", [writer stringWithObject:array]);

– 出力結果

1
2
3
4
5
6
7
8
9
10
[
  {
    "key00" : "value00",
    "key01" : "value01"
  },
  {
    "key10" : "value10",
    "key11" : "value11"
  }
]

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です