2014-04-04 29 views
0

我想匹配我的JSON NSString,我從另一個UITableView準備期間與兩個JSON包含相同的密鑰的NID匹配。 我想要的是讓這個UITableView只顯示匹配的nid內容。 我會從第二個UITableView發佈2套我的JSON和我的代碼。請幫幫我。JSON匹配NSString與NSDictionary

這裏是第1套myJSON

[ 
{ 
node_title: "Fumi", 
nid: "9", 
Body: "<p>Fumi Restaurant</p> ", 
Shop Branch Reference: "8", 
Shop Enterprise Reference: "<a href="/drupal/node/7">Fumi</a>", 
Shop Service Time: [ 
"<div class="time-default"> Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday 10:00am - 8:00pm</div> " 
], 
Shop User Reference: "<a href="/drupal/user/12">suppae</a>" 
}, 
{ 
node_title: "Shop Number One", 
nid: "3", 
Body: "<p>This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one This is body for shop number one</p> ", 
Shop Branch Reference: "2", 
Shop Enterprise Reference: "<a href="/drupal/node/1">Fuji</a>", 
Shop Service Time: [ 
"<div class="time-default"> Monday, Tuesday, Wednesday, Thursday, Friday 10:00am - 10:00pm</div> ", 
"<div class="time-default"> Saturday, Sunday 9:00am - 10:00pm</div> " 
], 
Shop User Reference: "<a href="/drupal/user/9">testshop</a>" 
} 
] 

的下面是第二盤我的JSON的

[ 
    { 
    node_title: "CTW", 
    nid: "8" 
    }, 
    { 
    node_title: "Siam Paragon", 
    nid: "2" 
    } 
    ] 

現在,我設法實現投其所好來解決這個問題。 現在我的問題是,我需要在匹配後爲我的json設置新值,以便它可以在UITableview上填充。

這裏是我的代碼

for(int i = 0; i < [_Json count]; i++) { 
     NSString * match =[[_Json valueForKey:@"nid"]objectAtIndex:i]; 
     NSString * branch =[self.detail valueForKey:@"Shop Branch Reference"]; 
     if([match isEqualToString:branch]) { 
      NSLog(@"Found "); 

非常感謝您的幫助。

+0

「我需要設置新的價值爲我的json匹配後,以便它可以填充在UITableview「不是一個很好的描述你的問題 - 你想要做什麼? (首先,請記住,你不是在處理JSON,而是在處理NSArrays和NSDictionarys。爲了「變異」一個,「設置」某些東西,它必須是「可變的」。所以,盡我所能理解你的問題,你想構建一個新的NSMutableArray,其中包含來自另外兩個數組的組合值。) –

+0

@HotLicks對不適的描述感到抱歉,是的,你理解我的問題。基本上,根據我的兩套JSON,我的第二個UITableView將只顯示與Shop Shop Reference相匹配的一個。感謝您的快速回復 – user3027109

+0

因此,構建一個包含組合值的新NSMutableArray(作爲新的NSMutableDictionarys)。或者在你現有的NSJSONSerialization調用中使用這個選項來返回「可變容器」,並修改其中一個數組與你的信息。 –

回答

0

首先,爲了節省您很多的煩惱:如果你把行的開頭{在行尾,Xcode會很好地格式化它,而不是把你的代碼放到屏幕的最右邊。

其次,您可以編寫一個方法來完成所有處理並從該塊中調用它,使您的代碼更具可讀性。

第三,如果我有字典的數組,並希望找到與鍵/值對「NID」的字典:「8」,我會寫

for (NSDictionary* dict in array) 
    if ([dict [@"nid"] isEqualToString:@"8"]) 
     NSLog (@"Found the dictionary!!!"); 
+1

我設法通過使用for(int i = 0; i <[_Json count]; i ++)來匹配我的json和string兩個字符串NSString * match = [[_ Json valueForKey:@「nid」] objectAtIndex:i] ; NSString * branch = [self.detail valueForKey:@「Shop Branch Reference」]; if([match isEqualToString:branch]){ NSLog(@「Found」); 但是,現在我需要在匹配後設置新的json值,以便可以將它們填充到我的UITableView中。 – user3027109

+0

你能幫我解決我目前的問題嗎? – user3027109