首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SBJson接口iPhone

SBJson接口iPhone
EN

Stack Overflow用户
提问于 2011-08-26 11:17:45
回答 1查看 484关注 0票数 0

我正在尝试从正向地理编码web服务API获取JSON数据。响应格式如下。

代码语言:javascript
复制
  [
    {"total":63},{
        "t":"1",
        "lable":"Gek Poh Shopping Centre",
        "address":"762 Jurong West Street 75. (S)640762",
        "street":"Jurong West Street 75",
        "zip":"640762",
        "long":"103.6980151847",
        "lat":"1.348986165348",
        "x":"355149.0357","y":
        "149142.5301",
        "is_prem":"0",
        "pid":"47120",
        "aid":"115810",
        "lid":"245690",
        "has_biz":"1",
        "is_main_building":"1",
        "id":"245690",
        "cat_id":"80"
        },
        {
        "t":"1",
        "lable":"Gek Poh Ville Community Club (CC)",
        "address":"1 Jurong West Street 74. (S)649149",
        "street":"Jurong West Street 74",
        "zip":"649149",
        "long":"103.69890252806",
        "lat":"1.3489703630875",
        "x":"355247.7723",
        "y":"149140.7302",
        "is_prem":"0",
        "pid":"2979",
        "aid":"116734",
        "lid":"127311",
        "has_biz":"1",
        "is_main_building":"1",
        "id":"127311",
        "cat_id":"14"
        }
    ]

这是我所做的。

代码语言:javascript
复制
-(IBAction)search:(id) sender{
    self.requestString = [NSString stringWithFormat:@"http://www.streetdirectory.com/api/?mode=search&act=all&profile=sd_default&q=%@&show_additional=0&output=json&limit=1", textField.text];
    NSString *escapedString = [self.requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:escapedString]];

    responseData = [[NSMutableData alloc] init];
    rConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *responseDict = [parser objectWithData:responseData];

    NSArray *resultsArray = [responseDict valueForKey:@""];   // am I doing it correctly to get the array?
    for (NSDictionary *childDic in resultsArray) {
        NSString *str = [childDic objectForKey:@"address"]; // for example I wanna get the address?
        label.text = str;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-26 11:34:33

你没有确切地说出你遇到了什么问题,所以我将从你发布的内容开始猜测。

在我看来,您的顶级JSON对象应该是一个包含3个字典的数组:

代码语言:javascript
复制
NSArray *responseArray = [parser objectWithData:responseData];

数组中的第一个字典似乎是一个只有一个合计值的字典:

代码语言:javascript
复制
NSDictionary *totalDict = [responseArray objectAtIndex:0];
NSLog(@"Total: %@", [totalDict objectForKey:@"total"]);

其余的字典似乎都有包含地址的记录:

代码语言:javascript
复制
for (int i = 1; i < [responseArray count]; i++) {
    NSDictionary *dict = [responseArray objectAtIndex:i];
    NSLog(@"Address %d = %@", i, [dict objectForKey:@"address"]);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7199669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档