2

とてもシンプルなはずですが、うまくいきません。

Json の応答は ([{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y": "4"}])

NSString *response = [request responseString];
//response is ([{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}])

SBJSON *parser = [[[SBJSON alloc] init] autorelease];

NSDictionary *jsonObject = [parser objectWithString:response error:NULL];
// jsonObject doesn't have any value here..Am I doing something wrong?

NSMutableArray Conversion = [jsonObject valueForKey:NULL];
//Even if I get the value of jsonObject. I don't know what to put for valueForKey here

変換には 2 つの NSObject が必要で、それぞれに次のようなものが必要です。

id:1 x:1 y:2

id:2 x:2 y:4

4

1 に答える 1

6

JSONパーサーは、NSDictionaryではなく、応答文字列からNSArrayを生成します。SBJSONを含むJSONパーサーは、解析されるjsonの内容に応じて、配列オブジェクトまたはディクショナリオブジェクトのいずれかを返すことに注意してください。

NSArray *jsonObject = [parser objectWithString:response error:nil];

次に、配列内の個々のアイテムにアクセスし(配列要素はNSDictionaryタイプになります)、を使用valueForKey:して各アイテムのプロパティを取得できます。

NSDictionary *firstItem = [jsonObject objectAtIndex:0];
NSString *theID = [firstItem objectForKey:@"id"];
于 2012-04-23T05:08:36.183 に答える