JSON将NSString与NSDictionary匹配

时间:2014-04-04 18:39:32

标签: ios iphone objective-c json uitableview

我试图匹配我在准备过程中从另一个UITableView获取的JSON NSString,以匹配两组JSON包含相同密钥的nid。 我想要的是使这个UITableView只显示匹配的nid内容。 我将从第二个UITableView发布2套我的JSON和我的代码。请帮帮我们。

这是第一套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 ");

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

首先,为了节省很多麻烦:如果你在行的末尾放置一个块的开头,Xcode会很好地格式化它,而不是将你的代码放在屏幕的最右边。

其次,您可以编写一个方法来执行所有处理并从块中调用它,从而使您的代码更具可读性。

第三,如果我有一个字典数组,并想找到一个带有键/值对“nid”的字典:“8”,我会写

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