从服务器读取 Json 响应并在 objective-c xcode 中解析

Read Json Response from server and parse in objective-c xcode

iOS 新生尝试解析我从服务器接收的 json 数据。 以下是我的 json 回复。

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

这里有使用 NSJSONSerialization 的想法吗?

我认为您是 objective-C 编程新手。我正在尝试解释您应该如何检索数据。

首先创建一个模型class。模型 class 对象将从服务器响应生成。

class应该是这样的,

YourModelClass.h 文件,

#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 文件,

#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

现在假设您已获得作为数据的服务器响应。

使用数据,您可以像这样填充模型对象,

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

我用下面的代码解决了这个问题。

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