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

Ios 从服务器读取Json响应并在objective-c xcode中解析,ios,objective-c,json,xcode,nsjsonserialization,Ios,Objective C,Json,Xcode,Nsjsonserialization,iOS中的新鲜人尝试解析我从服务器接收的json数据。 下面是我的json响应 { "msg": "success", "data": { "id": "1", "salutation": "Mr.", "first_name": "DIPAK NARANBHAI", "last_name": "PATEL", "email": "20xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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编程还不熟悉。我试图解释你应该如何检索数据

首先创建一个模型类。将根据服务器响应生成模型类对象

{
    "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"
    }
}
这个班级应该是这样的

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];
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
现在假设您获得了作为数据的服务器响应

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

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

我正在使用的以下代码可能重复:NSMutableArray*jsondata=[jsonObject objectForKey:@“data”];对于(jsondata中的NSDictionary*getdata){custfname=[getdata objectForKey:@“first_name”];custlname=[getdata objectForKey:@“last_name”];}但获取以下错误:[[Uu_NSCFConstantString objectForKey:]:根据您的服务器响应,无法识别的选择器发送到实例0x37E85A28,代码应该是这样的,我在您的服务器响应中没有找到任何数组,NSDictionary*jsonData=json[@“data”];客户名称=[jsonData objectForKey:@“姓氏”];客户名称=[jsonData objectForKey:@“姓氏”];