0

Youtubeプレイリストを解析しようとしています:

JSON が次のように構成されている場合:

{"apiVersion" .... 
"items":[{"id2":"some-id","title":"songtitle",

次の方法でタイトルを完全に解析できます。

// Fill array
NSArray *items = [json objectForKey:@"items"];

// Get item from tableData
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
// Set text on textLabel
[[cell textLabel] setText:[item objectForKey:@"title"]];

しかし、JSON が次のような場合:

{"apiVersion" .... 
"items":[{"id1": .... "video":{"id2":"some-id","title":"songtitle",

ネストされたオブジェクトのタイトルに到達するにはどうすればよいですか?

単純なことですが、私はこれについて何時間も頭を悩ませています。いらいらします、あなたの提案をありがとう!

[編集] これは完全な構造です:

{
    "items":
    [
    {
    "video":
            {
                "title": "Number One",
                "description": "Description one"
            },    
            {
             "title": "Number two",
                "description": "Description two"
            },
            {
                "title": "Number three",
                "description": "Description three"
            }
    },
    {   
    "video":
            {
         "title": "Number One",
             "description": "Description one"
            },
            {
                "title": "Number two",
              "description": "Description two"
            },
            {
               "title": "Number three",
              "description": "Description three"
            }
    }

    ]
}
4

3 に答える 3

3

これを試して:

NSArray *items = [[json valueForkey:@"items"]valueForkey:"video"];
于 2012-04-24T13:04:04.530 に答える
1
NSMutableArray *items = [json valueForkey:@"items"];
for(int i =0;i<[items count]; i++)
{
  NSMutableArray *arrtitle = [[[items objectAtIndex:i]valueForkey:@"Video"]copy];
  for(int j =0;j<[arrtitle count]; j++)
  {
    NSString *title = [[arrtitle objectAtIndex:j]valueForkey:@"title"];
  }
} 

多分それはあなたを助けるでしょう。

于 2012-04-24T12:43:50.703 に答える
0

問題は、jsonが無効であるということです。すべてtitlesが配列になっているはずですよね?したがって、[]に含まれている必要があります。たとえば、次のサイトを使用してjsonを「デバッグ」できます。

http://jsonformatter.curiousconcept.com/

また、iOS 5では、新しい組み込みのJSONAPIを使用できます。これはそのための素晴らしいチュートリアルです: http ://www.raywenderlich.com/5492/working-with-json-in-ios-5

それにもかかわらず、これがあなたのjsonが私が推測するように見えるべきである方法です:

{
   "items":[
      {
         "video":[
            {
               "title":"Number One",
               "description":"Description one"
            },
            {
               "title":"Number two",
               "description":"Description two"
            },
            {
               "title":"Number three",
               "description":"Description three"
            }
         ]
      },
      {
         "video":[
            {
               "title":"Number One",
               "description":"Description one"
            },
            {
               "title":"Number two",
               "description":"Description two"
            },
            {
               "title":"Number three",
               "description":"Description three"
            }
         ]
      }
   ]
}

あなたのプロジェクトで頑張ってください;)

于 2012-04-25T06:06:21.483 に答える