3

JSON Model Library

I have a Json object & respective models -

{
"type": "flight",
"senderid": "101",
"message": 
{
    "origin": "Bangalore",
    "destination": "Paris",
    "start_date": "21-04-2017",
    "end_date": "27-04-2017",
    "price": "300",
    "currency": "$",
    "fare_rules": "This is the fare rules text.",
    "refundable": "true",
    "check_in_baggage": "30 kg",
    "legs": [
        {
            "index": "1",
            "airline_id": "2003",
            "flight_number": "TK 721 T",
            "name": "Turkish Airline",
            "logo": "http://turkish.com/logo.png",
            "origin": "Bangalore",
            "destination": "Chennai",
            "start_date": "21-04-2017",
            "end_date": "21-04-2017",
            "start_time": "06.20 pm",
            "end_time": "8.15 pm",
            "journey_time": "2h 30m",
            "is_next": "true",
            "layover": "3 hours"
        },
        {
            "index": "2",
            "airline_id": "4003",
            "flight_number": "JT 021",
            "name": "Jet Airways",
            "logo": "http://jetairways.com/logo.png",
            "origin": "Chennai",
            "destination": "Paris",
            "start_date": "21-04-2017",
            "end_date": "21-04-2017",
            "start_time": "11.20 pm",
            "end_time": "2.15 am",
            "journey_time": "3h 10m",
            "is_next": "false",
            "layover": ""
        }
    ]
}
}

Mapping code -

DCParserConfiguration *config = [DCParserConfiguration configuration];

DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[TRChatFlightLegModel class] forAttribute:@"legs" onClass:[TRChatFlightMessageModel class]];

[config addArrayMapper:mapper];

DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[TRChatTypeFlightsModel class] andConfiguration:config];

TRChatTypeFlightsModel *chatTextModel = [parser parseDictionary:message.data.message];

And my model classes looks like this:

@interface TRChatTypeFlightsModel : NSObject

@property (strong, nonatomic) NSString *type;
@property (assign, nonatomic) NSInteger senderid;
@property (strong, nonatomic) TRChatFlightMessageModel *message;

@end

@interface TRChatFlightMessageModel : NSObject

@property (strong, nonatomic) NSString *origin;
@property (strong, nonatomic) NSString *destination;
@property (strong, nonatomic) NSString *start_date;
@property (strong, nonatomic) NSString *end_date;
@property (strong, nonatomic) NSString *fare_rules;
@property (strong, nonatomic) NSArray *flight_legs;

@interface TRChatFlightLegModel : NSObject

@property (strong, nonatomic) NSString *index;
@property (strong, nonatomic) NSString *airline_id;
@property (strong, nonatomic) NSString *is_next;
@property (strong, nonatomic) NSString *layover;

@end

I'm getting value for chatTextModel.message.origin but getting nil for chatTextModel.message.flight_legs. Any suggestions?

vaibhav
  • 4,038
  • 1
  • 21
  • 51
Dilip Lilaramani
  • 1,126
  • 13
  • 28

2 Answers2

1

Okay, here are the dict inside dict while legs is a dict which contains array of two elements.

You can fetch JSON data as simple as:

// returned string contaning JSON data
NSData *data = [returnString dataUsingEncoding:NSUTF8StringEncoding];
// json can be NSDictionary *json
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

// parsing data from main dict (JSON data)
NSDictionary *innerDict1 = [json valueForKey:@"message"];

// now parsing data from message dict
NSDictionary *innerDict2 = [innerDict1 valueForKey:@"legs"];

// parsing node of legs dict, can pass any key inside legs
NSLog(@"values: %@", [innerDict2 valueForKey:@"airline_id"]);

// getting values inside mutable array 
array1 = [innerDict2 valueForKey:@"index"];    
array2 = [innerDict2 valueForKey:@"airline_id"];
vaibhav
  • 4,038
  • 1
  • 21
  • 51
0

Change Model class object name "flight_legs" into "legs". Because in here
JSON Model Library does mapping

json key path <----> object key path.

Hence change

@property (strong, nonatomic) NSArray *flight_legs;

to

@property (strong, nonatomic) NSArray *legs; 
byJeevan
  • 3,728
  • 3
  • 37
  • 60