【问题标题】:parse JSON string to NSDictionary into array of objects Objective C将 JSON 字符串解析为 NSDictionary 成对象数组 Objective C
【发布时间】:2016-01-13 12:45:38
【问题描述】:

我是 Objective-c 的新手。它们是我的问题的许多解决方案,但我未能解决。我正在尝试解析 JSON 并且我成功完成了。代码如下:

     NSString *urlString   = [NSString stringWithFormat:@"%@",homePagesNews];
 // The Openweathermap JSON responder
        NSURL *url            = [[NSURL alloc]initWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLResponse *response;
        NSData *GETReply      = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        NSDictionary *res     = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",res);

输出是:

{
    "item_id": "102949",
    "featured_image": "http://198.72.115.125/~pratidin/assets/news_images/2015/10/05/thumbnails/VC_mohit_ul_alam_pic.jpg",
    "main_news_url": "http://198.72.115.125/~pratidin/api/detailnews/102949",
    "title": "'শিক্ষকতা চর্চাহীন একটা তন্ত্র গোষ্ঠিতে পরিণত হয়েছে'",
    "datetime": "2015-10-05 20:13:44",
    "summery": "'শিক্ষক সমাজ একেবারে চর্চাহীন একটা তন্ত্র…",
    "main_url": "http://198.72.115.125/~pratidin/api/categorynews/4"
  },
  {
    "item_id": "102198",
    "featured_image": "http://198.72.115.125/~pratidin/assets/news_images/2015/09/05/thumbnails/1438144816_104634.png.jpg",
    "main_news_url": "http://198.72.115.125/~pratidin/api/detailnews/102198",
    "title": "সাকার ছোট ছেলে হুম্মাম কারাগারে",
    "datetime": "2015-09-05 20:23:17",
    "summery": "রাজধানীর গুলশানে মারামারি অভিযোগে দায়ের…",
    "main_url": "http://198.72.115.125/~pratidin/api/categorynews/4"
  },
  {
    "item_id": "102180",
    "featured_image": "http://198.72.115.125/~pratidin/assets/news_images/2015/09/05/thumbnails/1_104567.jpg",
    "main_news_url": "http://198.72.115.125/~pratidin/api/detailnews/102180",
    "title": "আজ বীরশ্রেষ্ঠ নূর মোহাম্মদের ৪৪তম শাহাদতবার্ষিকী",
    "datetime": "2015-09-05 14:00:19",
    "summery": "মুক্তিযুদ্ধের রণাঙ্গণের সাহসী সন্তান…",
    "main_url": "http://198.72.115.125/~pratidin/api/categorynews/4"
  }

我想得到这个值 title,datetime,main_url 。我检查下面提到的 link .

提前致谢

【问题讨论】:

  • 试试这个方法NSLog(@"%@",[[res objectAtIndex:0] objectForKey:@"item_id"]);
  • 它已经是一个数组...尽管您的输出必须包含在两个方括号 [] 之间。与您在使用 NSLog 的调试控制台中看到的完全一样吗?

标签: ios objective-c json nsdictionary xcode7


【解决方案1】:

试试这个代码来获取价值

NSMutableArray *res     = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",res);
// get item id
NSLog(@"item-id :%@",[[res objectAtIndex:0] objectForKey:@"item_id"]);
NSLog(@"datetime %@",[[res objectAtIndex:0] objectForKey:@"datetime"]);
NSLog(@"main_url %@",[[res objectAtIndex:0] objectForKey:@"main_url"]);

或从响应中获取所有值

for (int i=0; i< res.count; i++){
    NSLog(@"item-id :%@",[[res objectAtIndex:i] objectForKey:@"item_id"]);
    NSLog(@"datetime %@",[[res objectAtIndex:i] objectForKey:@"datetime"]);
    NSLog(@"main_url %@",[[res objectAtIndex:i] objectForKey:@"main_url"]);
}

【讨论】:

  • 非常感谢 .. 效果很好 .. 我需要更多帮助 .. 你帮我个忙
  • 我将如何在我的自定义 UITableView..contact = contactsArray[indexPath.row] 中显示这些数据; NSString *firstName = 联系人[@"title"]; NSString *lastName = 联系人[@"summery"]; NSString *imageName = 联系人[@"featured_image"]; NSLog(@"现在:%@",firstName); UIImage *image = [UIImage imageNamed:imageName]; customCell.customFirstNameLabel.text = 名字; customCell.customLastnameLabel.text = 姓氏; customCell.customImageView.image =image;
  • 你可以这样显示数据 -> customCell.customFirstNameLabel.text = [[res objectAtIndex:i] objectForKey:@"title"]; @FerrakkemBhuiyan
  • @FerrakkemBhuiyan 对不起,我在公司。谷歌它。每件事嘿嘿。
  • @FerrakkemBhuiyan 参考这个例子appcoda.com/fetch-parse-json-ios-programming-tutorial
【解决方案2】:

您的响应是字典值的集合,因此您需要将响应存储在 NSArray 对象中,如下所示:

    NSArray *res     = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];

要获取属性,请使用以下代码:

    NSDictionary * dict = [res objectAtIndex:0];
    NSLog(@"title = %@", [dict objectForKey:@"title"]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2013-03-12
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多