-3

Fresher in iOS trying to parse the json data i am receiving from server. Below is my json response.

{
    "msg": "success",
    "data": {
        "id": "1",
        "salutation": "Mr.",
        "first_name": "DIPAK NARANBHAI",
        "last_name": "PATEL",
        "email": "20xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc@",
        "phone": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX3094",
        "vin_no": "SALVA2AN1HL921364",
        "lob": "Land Rover",
        "ppl": "Range Rover Evoque",
        "sub_model": "",
        "pl": "2.0 L TD4 132 kW Diesel 5 Door SE 5 Seater",
        "date_of_sale": "10-JUL-17",
        "account_name": "",
        "actual_delivery_date": "10-JUL-17",
        "selling_dealer": "CARGO MOTORS PVT LTD",
        "dlrid": "11201",
        "time": "1499773196",
        "stk_sync": "N",
        "vehicle_reg_no": "MH-01-AB1234"
    }
}

any ideas here using NSJSONSerialization?

  • Possible duplicate of [ios objective c parsing json](https://stackoverflow.com/questions/8407577/ios-objective-c-parsing-json) – Dharma Jul 15 '17 at 18:36

2 Answers2

1

I think you are new to objective-C programming. I'm trying to explain how you should retrieve the data.

First create a Model class. Model class object will be generated from server response.

The class should be look like this,

YourModelClass.h file,

#import <Foundation/Foundation.h>

@interface YourModelClass : NSObject

@property (nonatomic, strong) NSNumber *id;
@property (nonatomic, strong) NSString * salutation;
@property (nonatomic, strong) NSString * firstName;
@property (nonatomic, strong) NSString * lastName;
@property (nonatomic, strong) NSString * email;
————————
————————
————————
————————
@property (nonatomic, strong) NSString * vehicleRegNo;

- (instancetype)initWithDictionary: (NSDictionary *) dictionary;

@end

YourModelClass.m file,

#import "YourModelClass.h"

@implementation YourModelClass

- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self) {
        self.dictionary = dictionary;
    }
    return self;
}

- (void)setDictionary:(NSDictionary *)dictionary{
    self.id = dictionary[@“id”] ? : @“”;
    self.salutation = dictionary[@“salutation"] ? : @“”;
    self.firstName = dictionary[@"first_name"] ? : @“”;
    ————————
    ————————
    ————————
    ————————
    self.vehicleRegNo = dictionary[@"vehicle_reg_no"] ? : @“”;
}
@end

Now suppose you have get server response as Data.

With the data you can populate your model object like this,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:YourURL];

    [request setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {

        if (!error)
        {
            NSError *jsonError;

            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];

            if(!jsonError)
            {
                if([json[@"data"] isKindOfClass:[NSDictionary class]])
                {
                     YourModelClass *modelObject = [[YourModelClass alloc] initWithDictionary:json[@"data"]];
                }
            }

        }
    }];
    [task resume];
shuvo
  • 705
  • 5
  • 15
  • I am using the below code: NSMutableArray *jsondata = [jsonObject objectForKey:@"data"]; for (NSDictionary *getdata in jsondata) { custfname = [getdata objectForKey:@"first_name"]; custlname = [getdata objectForKey:@"last_name"]; } but getting following error: [__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x37e85a28 – Siddhesh damble Jul 18 '17 at 12:04
  • according to your server response, Code should be like this, I did not find any array in your server response, NSDictionary *jsonData = json[@"data"]; custfname =  [jsonData objectForKey:@"first_name”]; custlname =  [jsonData objectForKey:@"last_name”]; – shuvo Jul 18 '17 at 14:51
1

I solved the issue with following code.

 NSMutableDictionary *jsondata = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                NSMutableArray *jsonfname = [[jsondata objectForKey:@"data"] objectForKey:@"first_name"];
                NSMutableArray *jsonlname = [[jsondata objectForKey:@"data"] objectForKey:@"last_name"];