JSON 匹配 NSString 与 NSDictionary [英] JSON matching NSString with NSDictionary

查看:20
本文介绍了JSON 匹配 NSString 与 NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配我在 Preparetoseg 期间从另一个 UITableView 获得的 JSON NSString 以匹配两组 JSON 包含相同键的 nid.我想要的是让这个 UITableView 只显示匹配的 nid 内容.我将发布 2 组我的 JSON 和来自第二个 UITableView 的代码.请大家帮帮我.

这是第一组 myJSON

<预><代码>[{node_title: "Fumi",nid: "9",正文:<p>Fumi 餐厅</p>",店铺分行编号:8",店铺企业参考:"<a href="/drupal/node/7">Fumi</a>",店铺服务时间:["<div class="time-default"> 周一、周二、周三、周四、周五、周六、周日上午 10:00 - 晚上 8:00</div> "],商店用户参考:<a href="/drupal/user/12">suppae</a>"},{node_title: "一号店",nid: "3",Body: "<p>这是一号店的主体这是一号店的主体这是一号店的主体这是一号店的主体这是一号店的主体这是这是一号店的主体这是一号店的主体这是一号店的主体这是一号店的主体这是一号店的主体</p>店铺分行参考:2",店铺企业参考:"<a href="/drupal/node/1">Fuji</a>",店铺服务时间:["<div class="time-default"> 周一、周二、周三、周四、周五上午 10:00 - 晚上 10:00</div> ","<div class="time-default">周六、周日上午 9:00 - 晚上 10:00</div>"],商店用户参考:<a href="/drupal/user/9">testshop</a>"}]

这是我的第二组 JSON

<预><代码> [{node_title: "CTW",编号:8"},{node_title: "暹罗典范",编号: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(@"找到");

非常感谢您的帮助.

解决方案

首先,为了省去很多麻烦:如果你把块的开头 { 放在行尾,Xcode 会很好地格式化它并且不要把你的代码放在屏幕的最右边.

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

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

for (NSDictionary* dict in array)if ([dict [@"nid"] isEqualToString:@"8"])NSLog(@"找到字典了!!!");

I'm trying to match my JSON NSString that I got from another UITableView during the preparedtoseg to match up with the nid that both sets of JSON contain the same key. What I want is that to make this UITableView display only the matched nid content. I will post 2 sets of my JSON and my code from the 2nd UITableView. Please help me out guys.

Here is the 1st set of 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>"
}
]

Here is the 2nd set of my JSON

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

Now, I managed to implement the match up to solve the problem. my issue now is that I need to set the new value for my json after matching so that it can be populated on the UITableview.

Here is my code

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 ");

Thank you very much for your help.

解决方案

First, to save you a lot of aggravation: If you put the opening { of a block at the end of the line, Xcode will format it all nicely and not put your code to the extreme right of the screen.

Second, you are allowed to write a method to do all the processing and call it from the block, making your code a lot more readable.

Third, if I had an array of dictionaries, and wanted to find a dictionary with the key/value pair "nid":"8", I'd write

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

这篇关于JSON 匹配 NSString 与 NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆