0

私のJson応答は

[{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}]

そして、私はしました

 NSString *response = [request responseString];
 SBJSON *parser = [[SBJSON alloc] init];
 NSArray *jsonObject = [parser objectWithString:response error:nil];

この時点で、jsonObject は 2 つの NSDictionary を持つ配列であると思います。NSDictionary ではなく 2 つの NSArray を持つ jsonObject を作成するにはどうすればよいですか? そうするための最良の方法は何ですか?ネストされた NSDictionary を NSArray に変換する必要があると思いますか?

4

1 に答える 1

1

現在、FoundationフレームワークでNSJSONSerializationクラスを使用します。

JSONObjectWithDataメソッドを使用すると、データを最上位に格納するコンテナのタイプが返されます。例えば:

NSError *e;

NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e]; 

for (NSDictionary *dict in jsonObject) {

    NSLog(@"json data:\n %@", dict); 

    // do stuff

}

または、次のような可変コンテナを返すこともできます。

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];

Appleのドキュメントは次のとおりです。

NSJSONシリアル化

于 2012-04-23T23:38:08.240 に答える