-4

我是 iOS 开发新手。我有如下响应字符串:

{"7":"阿富汗","8":"阿尔巴尼亚","9":"阿尔及利亚","10":"安道尔","11":"安哥拉","12":"安提瓜和巴布达" ,"13":"阿根廷","14":"亚美尼亚","15":"奥地利","16":"阿塞拜疆","17":"巴哈马","18":"巴林"," 19":"孟加拉国","20":"巴巴多斯","21":"白俄罗斯","22":"比利时","23":"伯利兹","24":"贝宁","25" :"不丹","26":"玻利维亚","27":"波斯尼亚和黑塞哥维那","28":"博茨瓦纳","29":"巴西","30":"文莱","31":"保加利亚","32":"布基纳法索","33":"布隆迪","34":"柬埔寨","35":"喀麦隆","36": "加拿大","37":"佛得角","38":"中非共和国","39":"乍得","40":"智利","41":"中国","42" :"哥伦比亚","43":"科摩罗","44":"哥斯达黎加","45":"科特迪瓦","46":"克罗地亚","47":"古巴"," 48":"塞浦路斯","49":"捷克共和国","50":"刚果民主共和国","51":"丹麦","52":"吉布提","53":"多米尼克","54":"多米尼加共和国","55":"东帝汶","56":"厄瓜多尔","57":"埃及","58":"萨尔瓦多","59":"赤道几内亚","60":"厄立特里亚","61":"爱沙尼亚","62":"埃塞俄比亚"}

请帮我获取国家名单。

4

3 回答 3

2

尝试下面的代码来解析 iOS 5 以上的 JSON 数据

NSError *jsonError = nil;
NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:JsonData options:0 error:&jsonError]; 
// here JsonData is nsmutabledata in which your response is append 
NSLog(@"%@",tempDict);

试试这个你会成功使用 iOS 5.0 和 5.0 以上

于 2013-03-28T11:56:51.530 回答
0

我想你不知道 JSON 解析。

在谷歌搜索“iOS JSON Parsing”,你会得到很多教程。

让我在这里为您列出一些。

1. 在 iOS 5 教程
2 中使用 JSON。 iPhone 3的 JSON 框架。如何在 iOS App 中解析 JSON

学习 JSON 解析尝试在您的应用程序中进行,如果您遇到问题,请在此处发布。

于 2013-03-28T11:59:34.130 回答
0

您首先需要执行以下操作:将此代码嵌入到 viewdidLoad 函数中:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:your URL path for json file"]];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

那么你必须添加这个功能:

-(void)fetchedData:(NSData *)responseData{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
jsonResults = [json objectForKey:@"Content"];
}

[self.tableView reloadData];

}

希望对你有帮助

于 2013-03-28T12:39:11.817 回答