2017-07-15 68 views
-4

嘗試解析從服務器接收的json數據的iOS中的新手。 以下是我的json響應。從服務器讀取Json響應並在objective-c中解析xcode

{ 
    "msg": "success", 
    "data": { 
     "id": "1", 
     "salutation": "Mr.", 
     "first_name": "DIPAK NARANBHAI", 
     "last_name": "PATEL", 
     "email": "2[email protected]", 
     "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的任何想法?

+0

的可能的複製[IOS目標C解析JSON(https://stackoverflow.com/questions/8407577/ios-objective-c-parsing-json) –

回答

0

我解決了下面的代碼的問題。

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

我認爲你是新的Objective-C編程。我試圖解釋你應該如何檢索數據。

首先創建一個Model類。模型類對象將從服務器響應生成。

類應該是這樣的,

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

我使用以下代碼: 的NSMutableArray * jsondata = [jsonObject objectForKey:@「data」]; for(NSDictionary * getdata in jsondata) { custfname = [getdata objectForKey:@「first_name」]; custlname = [getdata objectForKey:@「last_name」]; } 但得到以下錯誤: [__NSCFConstantString objectForKey:]:無法識別的選擇根據您的服務器響應發送到實例0x37e85a28 –

+0

,代碼應該是這樣的,我沒有在你的服務器響應發現任何陣列, NSDictionary * jsonData = json [@「data」]; custfname = [jsonData objectForKey:@「first_name」]; custlname = [jsonData objectForKey:@「last_name」]; – shuvo