0

私はiphoneとjsonの世界では初めてです。私はこのjson構造を持っています。http://jsonviewer.stack.hu/に置くとはっきりとわかります。

 {"@uri":"http://localhost:8080/RESTful/resources/prom/","promotion":[{"@uri":"http://localhost:8080/RESTful/resources/prom/1/","descrip":"description
 here","keyid":"1","name":"The first
 name bla bla
 ","url":"http://localhost/10.png"},{"@uri":"http://localhost:8080/RESTful/resources/promo/2/","descrip":"description
here","keyid":"2","name":"hello","url":"http://localhost/11.png"}]}

json-framework で解析したい。私はこれを試しました

 NSDictionary *json    = [myJSON JSONValue];
            NSDictionary *promotionDic    = [json objectForKey:@"promotion"];
            NSLog(@" res %@ : ",[promotionDic objectAtIndex:0]); 

しかし、たとえば、インデックス 0 のオブジェクトの名前を取得するにはどうすればよいでしょうか。オブジェクトを NSArray に入れるべきだと思いますか? しかし、私は方法を見つけられません:/そして、オブジェクトの数が可変ではありません。助けてください 。

4

2 に答える 2

2

まず、JSONを読み込んだ後にこの行が必要です

        NSLog(@" json %@ : ",[json description]); 

それはあなたが持っているものを教えてくれます。

Secondly, you can't call objectAtIndex: on a dictionary. If it works, its because promotionDict is really an NSArray. (The NSLog will tell you). In Objective - C you can assign to any kind of pointer, which is confusing to new developers, but is part of the language, and really is a feature.

With an NSArray you can ask for the number of things in it, [myArray count], etc. You need to command double click in XCode to open up the docs for NSDictionary, etc.

于 2011-05-22T18:21:38.777 に答える
0

JSON は次のように述べています。

..."promotion":[{"@u...

" [" ということは、"プロモーション" が辞書ではなく配列にキー付けされていることを意味します。

したがって、実際にあなたのコードは次のようになります。

NSDictionary *json    = [myJSON JSONValue];
NSArray *promotions   = [json objectForKey:@"promotion"];
NSLog(@"res: %@",[promotions objectAtIndex:0]); 
于 2011-05-22T19:09:01.363 に答える