1

[{first set of values},{data->children->data->body}目標 cで json のこの部分をループするにはどうすればよいですか?

Jsonは

  [
      {
        "kind": "Listing"
      },
      {
        "kind": "Listing",
        "data": {
          "children": [
            {
              "data": {
                "body": "body1"
              }
            },

            {
              "data": {
                "body": "body2"
              }
            }
          ]
        }
      }
    ]

私の現在のコードは

m_ArrList=[[NSMutableArray alloc]init];
    NSDictionary *infomation = [self dictionaryWithContentsOfJSONString:@"surveyquestion.json"];
    NSArray *array=[infomation objectForKey:@"data"];
    int ndx;
    NSLog(@"%@",array);
    for (ndx = 0; ndx < [array count]; ndx++) {
        NSDictionary *stream = (NSDictionary *)[array objectAtIndex:ndx];

        NSArray *string=[stream valueForKey:@"children"];

        //i am stuck here
    }

「//私はここで立ち往生しています」で何をしますか?

4

3 に答える 3

1

辞書の値を配列に追加し、 @"children"その配列を解析して子の内部のデータを取得する必要がある場合があります

[childrenArray addObject:[stream objectForKey:@"children"]];

   // finally parse childrenArray
于 2013-03-19T10:36:11.830 に答える
0
// You Just need to Implement following Lines and you will get all the data for Key Body in children array
NSDictionary *infomation = [self dictionaryWithContentsOfJSONString:@"surveyquestion.json"];

NSArray *string= [[infomation objectForKey:@"data"] objectForKey:@"children"];

[string enumerateObjectsUsingBlock:^(id obj, NSUInteger ind, BOOL *stop){
    NSLog(@"Body : %@",[[obj objectForKey:@"data"] objectForKey:@"body"]);
}];
于 2013-03-19T10:53:26.250 に答える
0

NSJSONSerializationtry を使用してこれを実装します。ここでNSStringは、ファイルから読み取る必要がある jsonStr として渡す必要があります。

NSError *jsonError = nil;
id allValues = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]
                                               options:0
                                                 error:&jsonError];

if(jsonError!=nil)
    NSLog(@"Json_Err: %@",jsonError);

NSArray *array=allValues;

for (int ndx = 0; ndx < [array count]; ndx++) {
    NSDictionary *stream = (NSDictionary *)[array objectAtIndex:ndx];
    NSLog(@"%@",[stream objectForKey:@"kind"]);

    NSArray *child = [[stream objectForKey:@"data"] objectForKey:@"children"];

    //i am stuck here

    for(int i =0; i <[child count];i++)
    {
        NSDictionary *childData = (NSDictionary *)[child objectAtIndex:i];
        //NSLog(@"%@",[childData objectForKey:@"data"]);
        NSLog(@"%@",[[childData objectForKey:@"data"] objectForKey:@"body"]);
    }
}
于 2013-03-19T11:20:58.303 に答える