2011-11-25 46 views
2

我有一個JSON(Key和Value方法)格式的webservice響應。我解析並獲得最大的webservice方法。但是,在一個Web服務方法中,我無法從密鑰中獲取值。在這裏,我安裝的樣本響應,如何在iPhone中使用NSDictionary/NSMutableDictionary中的鍵而不使用鍵?

lessons =  (
       { 
      "ObjectiveC Book" =    (
           { 
        "brief_desc" = "ObjectiveC"; 
        cost = 20; 
        date = "2011-09-06"; 
       }, 
           { 
        "brief_desc" = "ObjectiveC"; 
        cost = 20; 
        date = "2011-09-07"; 
       }, 
           { 
        "brief_desc" = "ObjectiveC"; 
        cost = 20; 
        date = "2011-09-09"; 
       }, 
           { 
        "brief_desc" = "ObjectiveC"; 
        cost = 20; 
        date = "2011-09-10"; 
       }, 
           { 
        "brief_desc" = "ObjectiveC"; 
        cost = 20; 
        date = "2011-09-14"; 
       } 
      ); 
     } 
    ); 

我已經得到了價值的主要經驗,值,

{ 
       "ObjectiveC Book" =    (
            { 
         "brief_desc" = "ObjectiveC"; 
         cost = 20; 
         date = "2011-09-06"; 
        }, 
            { 
         "brief_desc" = "ObjectiveC"; 
         cost = 20; 
         date = "2011-09-07"; 
        }, 
            { 
         "brief_desc" = "ObjectiveC"; 
         cost = 20; 
         date = "2011-09-09"; 
        }, 
            { 
         "brief_desc" = "ObjectiveC"; 
         cost = 20; 
         date = "2011-09-10"; 
        }, 
            { 
         "brief_desc" = "ObjectiveC"; 
         cost = 20; 
         date = "2011-09-14"; 
        } 
       ); 
      } 

現在,我需要得到的鑰匙日期,成本價值和brief_desc。上面的值在NSDictionary中。所以,我使用這段代碼來獲取關鍵日期,成本和bried_desc的值。

NSDictionary *lessonDic = [[NSDictionary alloc] init]; 
    lessonDic = [latestLoans valueForKey:@"lessons"]; 
    NSLog(@"LessonsDic : %@", lessonDic); 

    NSMutableDictionary *BookDic = [[NSMutableDictionary alloc] init]; 
    BookDic = [lessonDic valueForKey:@"ObjectiveC Book"]; 
    NSLog(@"StudentDic : %@, Count : %d", BookDic, [BookDic count]); 

    NSArray *sDateArray = [BookDic valueForKey:@"date"]; 
    NSLog(@"sDateArray : %@", sDateArray); 

在NSlog中我得到了字典方法中的值。 sDateArray的值和計數是,

(
     "2011-09-06", 
     "2011-09-07", 
     "2011-09-09", 
     "2011-09-10", 
     "2011-09-14" 
    ) 
count : 1 

我該如何解析並獲取關鍵日期,brief_desc和成本的確切值?我需要在UITableView中加載這些項目。請幫我解決這個問題。提前致謝。

回答

3

感謝所有看過我的問題並回答我的朋友。我用這段代碼解決了這個問題。我特別感謝馬蒂亞斯瓦德曼先生,阿德博伊先生和諾蒂先生。

NSArray *items = [lessonDicti valueForKeyPath:@"ObjectiveC Book"]; 
NSLog(@"Items : %@, Count :%d", items, [items count]); 
NSEnumerator *enumerator = [items objectEnumerator]; 
NSDictionary* item; 
while (item = (NSDictionary*)[enumerator nextObject]) 
{ 
    NSLog(@"Item Dict : %@", item); 
    sDateArray = [item valueForKey:@"date"]; 
    NSLog(@"sDateArray : %@", sDateArray); 
    descArray = [item valueForKey:@"brief_desc"]; 
    cstArray = [item valueForKey:@"cost"]; 
    NSLog(@"eventStatusArray : %@, Count : %d", cstArray, [cstArray count]); 
} 

此代碼已解決我的問題。謝謝。

+0

有沒有什麼方法可以線性迭代? –

0

我認爲您在NSArray上使用valueForKey,您認爲這是NSMutableDictionary。結果將是valueForKey發送到數組中的所有元素一個新的數組與返回值。

[lessonDic valueForKey:@"ObjectiveC Book"];是一個數組而不是字典。

順便說一句,在使用它們之前,您不需要爲lessonDicBookDic執行alloc/init。你應該讀一下關於Objective-C和Cocoa內存管理的內容。

+0

感謝現場迴應。我也試過這個代碼,NSDictionary * sDateArray = [BookDic valueForKey:@「date」]; NSLog(@「sDateArray:%@」,sDateArray);也厭倦了NSMutableDictionary,這兩種情況下它都會返回與我在我的問題(NSlog)中提到的相同的值。 –

+0

我試圖通過這種方式獲得所有密鑰和值, (id key in studentDic) { NSLog(@「Key:%@」,key); //這裏的密鑰返回整個ObjectiveC Book值。 id value = [studentDic valueForKey:key]; //在這一行中,應用程序崩潰。 NSLog(@「Value:%@」,value); } –

+0

請注意,'valueForKey'返回一個'id'類型,因此如果您指定爲「錯誤」類型,編譯器將不會給出警告。您需要使用'isKindOfClass'在運行時檢查類型。在這種情況下,它會工作,因爲NSArray和NSDictionary(以及相互版本)都響應'valueForKey'選擇器。否則,你會得到一個關於無法識別的選擇器的運行時異常。 –

0

我認爲你應該使用像字典組成的陣列:

//in you .h 
NSArray objectiveBook 

然後初始化它在你的.m並得到JSON。喜歡的東西:

objectiveBook = [lessonDic valueForKey:@"ObjectiveC Book"]; 

這樣就可以在的tableView

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
return [objectiveBook count]; } 

而在定製表視圖單元格的外觀使用它的大小:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"cell"; 

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
//blah blah.. 

    NSDictionary *temp = [objectiveBook objectAtIndex:indexPath.row]; 

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 170, 60)]; 
    //initialize other labels here... 

//gets the values you want 
lbl1.text = [temp objectForKey:@"brief_desc"]; 
lbl2.text = [temp objectForKey:@"cost"]; 
lbl3.text = [temp objectForKey:@"date"]; 
//display it 
[cell.contentView addSubview:lbl1]; 
[cell.contentView addSubview:lbl2]; 
[cell.contentView addSubview:lbl3]; 

return cell; } 

你去那裏。我假設你已經在你的筆尖中設置了uitableview。

+0

謝謝Mr.Adedoy。我會在我的代碼中嘗試你的好主意。非常感謝。 –

+0

對不起朋友。我已經試過你的代碼,但是,ObjectiveBook數組返回了計數'0'。所以,應用程序崩潰在這一行,NSDictionary * temp = [objectiveBook objectAtIndex:indexPath.row];任何更多的建議/想法。我盡力解決這個問題,但我失敗了。 –

0

可以使用allKeysallValues方法

,如果你願意,你可以用下面的例子

static NSInteger alphabeticSortByLabelInDictionary_pad(NSDictionary *dict1, NSDictionary *dict2, void *reverse) { 
    NSString *label1 = [dict1 objectForKey:@"Label"]; 
    NSString *label2 = [dict2 objectForKey:@"Label"]; 

    if (*(BOOL *)reverse == YES) { 
     return [label2 localizedCaseInsensitiveCompare:label1]; 
    } 
    return [label1 localizedCaseInsensitiveCompare:label2]; 
} 

filteredListContent按名稱進行排序鍵 - NSArray的

使用例如:

BOOL reverseSort = NO; 
    list = [filteredListContent sortedArrayUsingFunction:alphabeticSortByLabelInDictionary_pad context:&reverseSort]; 
相關問題