【问题标题】:Read Json Response from server and parse in objective-c xcode从服务器读取 Json 响应并在 objective-c xcode 中解析
【发布时间】:2023-04-07 19:44:01
【问题描述】:

iOS 中的 Fresher 试图解析我从服务器接收到的 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 的想法吗?

【问题讨论】:

标签: ios objective-c json xcode nsjsonserialization


【解决方案1】:

我认为您是 Objective-C 编程的新手。我试图解释你应该如何检索数据。

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

类应该是这样的,

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

【讨论】:

  • 我正在使用以下代码: NSMutableArray *jsondata = [jsonObject objectForKey:@"data"]; for (NSDictionary *getdata in jsondata) { custfname = [getdata objectForKey:@"first_name"]; custlname = [getdata objectForKey:@"last_name"];但得到以下错误:[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x37e85a28
  • 根据你的服务器响应,代码应该是这样的,我在你的服务器响应中没有找到任何数组,NSDictionary *jsonData = json[@"data"]; custfname = [jsonData objectForKey:@"first_name"]; custlname = [jsonData objectForKey:@"last_name”];
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-09
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多