Rehan K
Rehan K

Reputation: 193

AFNetworking GET json values and save it NSMutablearray

I'm new to ios and its developing. i have clean code with set Correctly AFNetworking.My base URl's json Encording has got JSON objects and arrays as well as values . in my JSON out put i want to get values of "thumbnail" every time i do im getting Null .please help me to get "name ,thumbnail,id,images " of my json output. please find my NSDictionary type printed object's NSlog.

2014-07-20 09:08:33.110 WADTourisum[1157:60b] Reachability Flag Status: -R ------- networkStatusForFlags
2014-07-20 09:08:33.879 WADTourisum[1157:60b] JSON: {
    Main =     (
                {
            id = 1;
            "image_bundle_id" = 1;
            images =             (
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/1.jpg",
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/2.jpg",
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/3.jpg"
            );
            name = OCEAN;
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/ocean.jpg";
        },
                {
            id = 2;
            "image_bundle_id" = 23;
            images =             (
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/1.jpg",
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/2.jpg",
                "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/3.png"
            );
            name = Heritage;
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/heritage.jpg";
        },
                {
            id = 3;
            "image_bundle_id" = 0;
            images =             (
            );
            name = "Tea Country";
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/teaCountry.jpg";
        },
                {
            id = 4;
            "image_bundle_id" = 0;
            images =             (
            );
            name = "WILD LIFE";
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/wildLife.jpg";
        },
                {
            id = 5;
            "image_bundle_id" = 0;
            images =             (
            );
            name = Culture;
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/culture.jpg";
        },
                {
            id = 6;
            "image_bundle_id" = 0;
            images =             (
            );
            name = "NIGHT LIFE";
            thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/nightLife.jpg";
        }
    );

}

my code snippit

-(void) retriveData
{



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:@"http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php"
     parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

         self.posts =(NSDictionary *)responseObject;
         self.post =self.posts[@"thumbnail"];




        NSLog(@"JSON: %@", self.post);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please log into internetet"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];







}

Upvotes: 2

Views: 1089

Answers (1)

Shubhendu
Shubhendu

Reputation: 1091

You are getting the response and the only problem i see is you are not able to retrieve values correctly.

While fetching data from JSON remember that in which format you are getting the data i.e. either you are getting arrays or dictionary.

Seeing your response you are getting Array which in itself contains dictionary.

use the below code to fetch the values

NSArray *array = [responseObject valueForKey:@"Main"];

    for (NSDictionary *dict in array) {
        NSInteger ids = [[dict valueForKey:@"id"] integerValue];
        NSString *name = [dict valueForKey:@"name"];
        NSString *thumbnail = [dict valueForKey:@"thumbnail"];
        NSArray *arrImages = [dict valueForKey:@"images"];
        //You can use them accordingly

    } 

Hope this helps you. Happy coding :)

Upvotes: 4

Related Questions