2012-06-04 32 views
4

here是我的數據模式,我想要創建一個數組部分,在這種情況下,每個部分將在該部分包含一個行數組。我看了看下面的問題:如何解析NSDictionary到數組數組中?

和其他幾個職位,但也拿不出結果。任何人都可以請指導我如何創建部分的陣列,並且每個部分應包括其自己的行..謝謝提前

編輯 我有媒體鏈接解析我的數據,在引擎收錄鏈接日誌結果是的NSDictionary 這裏是我取&解析它:

NSString*key1=[ result objectForKey:@"key" ];     
     NSString *s1=[@"http://" stringByAppendingString:server.text]; 
     NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"]; 
     NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]]; 

     NSData *data2=[NSData dataWithContentsOfURL:url2]; 
     result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil]; 

在引擎收錄結果是這樣的:NSLog(@"Result: %@", result2);

+0

@Jack,我也跟着你的答案,但不可能得到任何結果,U可以提供一些請多幫忙? – ilhnctn

+0

我沒有給出任何答案,我只是重新格式化了你的問題 –

+0

啊對不起,第三個答案是[this](http://stackoverflow.com/questions/724772/2d-arrays-in-objective-c)有人叫傑克。我想他是你:) – ilhnctn

回答

1

你的數據在JSON格式,因此就使用NSJSONSerialization類來爲你創建數組的數組。

NSString *yourData; 
NSData *data = [yourData dataUsingEncoding:NSUTF8StringEncoding]; 
NSArray *arrayOfArrays = [NSJSONSerialization JSONObjectWithData:data 
    options:NSJSONReadingAllowFragments error:nil]; 

要使用部分的排列在一個表視圖,只需使用普通的表格視圖datasource方法:

// numberOfSectionsInTableView 
return [array count]; 

// numberOfRowsInSection 
return [[array objectAtIndex:section] count]; 

// cellForRowAtIndexPath 
cell.textLabel.text = [[[array objectAtIndex:indexPath.section] 
    objectAtIndex:indexPath.row] 
     objectForKey:"name"]; 
+0

我已經使用NSJSONSerialization解析了數據,但我會嘗試你的代碼 – ilhnctn

+0

返回null。我已經在 – ilhnctn

+0

之前使用過NSJSONSerialization,然後使用一個錯誤變量'NSError * error;'像這樣'... error:&error]; NSLog(@「%@」,錯誤);'看看它說什麼。 – Mundi