0

I am bit confused in fetching data and displaying data from json into my App using Model.

I am having this kind of json data :

{
result 
 [
    {
        key : value
        key : value
        key : value
    }
    {
        key : value
        key : value
        key : value
    }
 ]

}

I am trying this kind of code:

json = [[NSMutableArray alloc]init];
            NSError *writeError = nil;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&writeError];

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&writeError];


            json = dic[@"Result"];
            int i;
            for (i = 0; i <= json.count; i++)
            {
                NSMutableArray *array = json[i];
                [json enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
                 {
                     // Dim = [[DimEntityFull alloc]initWithJSONDictionary:obj];
                      saveSearch = [[SaveSearchMaster alloc]initWithJSONDictionary:obj];
                 } ];
            }

I am using "AFNetworking" and I am trying to fetch data and store into model class and then display to custom cell labels.

How can I get it.

Thank You.

3
  • Have you tried logging the data at each stage of the extraction? Commented Oct 5, 2015 at 5:19
  • yes, but it's not working for me. i am bit confusing in how to store into the model class. Commented Oct 5, 2015 at 5:24
  • i know that arrayOfRecords = [jsonData objectForKey:@"result"]; and then cell.lblName.text = [[arrayOfRecords objectAtIndex:indexPath.row]valueForKey:@"name"]; is the right flow. but with model class how can i get the same result? Commented Oct 5, 2015 at 5:27

1 Answer 1

2

In your view controller

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self getUsersList];
}

-(void)getUsersList
{



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager POST:[NSString stringWithFormat:@"http://www.yourdomainname.com/getUserList"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {

        //we will add Modal class objects of users in this array

        usersArray=[[NSMutableArray alloc]init];

        //getting result dictionary from response
        NSDictionary *resultDictinary = [responseObject objectForKey:@"result"];
        for (NSDictionary *userDictionary in resultDictinary)
        {
            //allocating new user from the dictionary
            User *newUSer=[[User alloc]initWithDictionary:userDictionary];
            [usersArray addObject:newUSer];
        }

        //in users array, you have objects of User class

        //reload your tableview data
        [self.TableView reloadData];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

}

Now Create New file Called 'User'
in User.h

@interface User : NSObject
{
    NSString *userID;
    NSString *firstName;
    NSString *lastName;

}
@property (nonatomic, retain)NSString *userID;
@property (nonatomic, retain)NSString *firstName;
@property (nonatomic, retain)NSString *lastName;

-(id)initWithDictionary:(NSDictionary *)sourceDictionary;


@end

in User.m
#import "User.h"

@implementation User
@synthesize userID,firstName,lastName;


-(id)initWithDictionary:(NSDictionary *)sourceDictionary
{
    self = [super init];
    if (self != nil)
    {
        self.firstName=[sourceDictionary valueForKey:@"firstName"];
        self.lastName=[sourceDictionary valueForKey:@"lastName"];
        self.userID=[sourceDictionary valueForKey:@"userID"];
    }
    return self;

}
@end

in your numberOfRowsInSectionmethod
return usersArray.count;

in your cellForRowAtIndexPath method

User *user=(User *)[usersArray objectAtIndex:indexPath.row];
yourLabel.text=user.firstName;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.