1

I am new in objective-c . They are many solutions for my problem but I am failed to solve .I am trying to parsing a JSON and i successfully done. Code are bellow :

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

Output are :

{
    "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"
  }

I want to get this value title,datetime,main_url . I check bellow mention link .

Thanks in advance

Community
  • 1
  • 1
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
  • 2
    Try this way `NSLog(@"%@",[[res objectAtIndex:0] objectForKey:@"item_id"]);` – Dharmesh Dhorajiya Oct 15 '15 at 07:16
  • It is already an array... although your output must have been enclosed between two square brackets `[]`. Is it exactly what you see in your debug console using NSLog? – sargeras Oct 15 '15 at 07:19

2 Answers2

3

Try this code to get value

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

or get all value from response

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"]);
}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
  • thanks a lot .. it works perfectly .. i need one more help .. you u kindly do me favour – Ferrakkem Bhuiyan Oct 15 '15 at 07:30
  • how i will show this data in my custom UITableView..contact = contactsArray[indexPath.row]; NSString *firstName = contact[@"title"]; NSString *lastName = contact[@"summery"]; NSString *imageName = contact[@"featured_image"]; NSLog(@"Now : %@",firstName); UIImage *image = [UIImage imageNamed:imageName ]; customCell.customFirstNameLabel.text = firstName; customCell.customLastnameLabel.text = lastName; customCell.customImageView.image =image; – Ferrakkem Bhuiyan Oct 15 '15 at 07:39
  • you can show data this way -> `customCell.customFirstNameLabel.text = [[res objectAtIndex:i] objectForKey:@"title"];` @FerrakkemBhuiyan – Dharmesh Dhorajiya Oct 15 '15 at 08:35
  • 1
    @FerrakkemBhuiyan sorry I am in company. goole it. every thing hehe. – Dharmesh Dhorajiya Oct 15 '15 at 10:08
  • 1
    @FerrakkemBhuiyan refer this example http://www.appcoda.com/fetch-parse-json-ios-programming-tutorial/ – Dharmesh Dhorajiya Oct 15 '15 at 10:13
1

Your response is collection of dictionary values, so you need to store your response in NSArray object like below :

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

To fetch attributes, use below code:

    NSDictionary * dict = [res objectAtIndex:0];
    NSLog(@"title = %@", [dict objectForKey:@"title"]);
Himanshu Mahajan
  • 4,779
  • 2
  • 36
  • 29