我正在使用 dchohfi 开发的 KeyValueObjectMapping。但在映射 JSON 对象数组时面临问题
I'm using KeyValueObjectMapping developed by dchohfi. But facing issue in mapping JSON object array
我有一个 Json 对象和各自的模型 -
{
"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": ""
}
]
}
}
映射代码 -
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];
我的模型 类 看起来像这样:
@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
我得到 chatTextModel.message.origin 的价值,但 chatTextModel.message.flight_legs 得到 nil
的价值。有什么建议吗?
好的,这是dict里面的dict,而legs
是一个包含两个元素数组的dict。
您可以像这样简单地获取 JSON
数据:
// 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"];
将模型 class 对象名称“flight_legs”更改为“legs”。因为在这里
JSON模型库做映射
json key path <----> object key path.
因此改变
@property (strong, nonatomic) NSArray *flight_legs;
到
@property (strong, nonatomic) NSArray *legs;
我有一个 Json 对象和各自的模型 -
{
"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": ""
}
]
}
}
映射代码 -
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];
我的模型 类 看起来像这样:
@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
我得到 chatTextModel.message.origin 的价值,但 chatTextModel.message.flight_legs 得到 nil
的价值。有什么建议吗?
好的,这是dict里面的dict,而legs
是一个包含两个元素数组的dict。
您可以像这样简单地获取 JSON
数据:
// 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"];
将模型 class 对象名称“flight_legs”更改为“legs”。因为在这里
JSON模型库做映射
json key path <----> object key path.
因此改变
@property (strong, nonatomic) NSArray *flight_legs;
到
@property (strong, nonatomic) NSArray *legs;