[英]Parsing nested json in iOS using Mantle
I'm trying to parse data received from a service using the framework Mantle. 我正在尝试使用框架Mantle解析从服务接收的数据。 The json has nested data and I am having problems to parse it. json嵌套了数据,我在解析数据时遇到了问题。 The json is like the following: json如下所示:
{
"sections": [
{
"title": "title1",
"level": 1,
"content": [
{
"type": "type1",
"text": "text1"
}
],
"images": []
},
{
"title": "title2",
"level": 2,
"content": [
{
"type": "type2",
"text": "text2"
},
{
"type": "type9",
"text": "text9"
},
{
"type": "type4",
"text": "text4"
},
{
"type": "type6",
"text": "text6"
}
],
"images": [
{
"src": "http://cvbcvcv",
"caption": "caption"
}
]
}]
}
The class that I am using is: 我正在使用的类是:
// MainObject.h
@interface MainObject : MTLModel <MTLJSONSerializing>
@property (strong, nonatomic) NSArray *sectionsArray;
+ (NSValueTransformer *)sectionsArrayJSONTransformer;
@end
@interface Section : MTLModel <MTLJSONSerializing>
@property (strong, nonatomic) NSString *title;
@property (assign, nonatomic) NSString *level;
@property (strong, nonatomic) NSArray *content;
@property (strong, nonatomic) NSArray *images;
+ (NSValueTransformer *)contentJSONTransformer;
+ (NSValueTransformer *)imagesJSONTransformer;
@end
@interface Content : MTLModel <MTLJSONSerializing>
@property (strong, nonatomic) NSString *type;
@property (strong, nonatomic) NSString *text;
@end
@interface Image : MTLModel <MTLJSONSerializing>
@property (strong, nonatomic) NSString *src;
@property (strong, nonatomic) NSString *caption;
@end
and 和
//MainObject.m
@implementation MainObject
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"sectionsArray" : @"sections",};
}
+ (NSValueTransformer *)sectionsArrayJSONTransformer
{
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[Section class]];
}
@end
@implementation Section
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"title" : @"title",
@"level" : @"level",
@"content" : @"content",
@"images" : @"images",};
}
+ (NSValueTransformer *)contentJSONTransformer
{
return [MTLJSONAdapter arrayTransformerWithModelClass:[Content class]];
}
+ (NSValueTransformer *)imagesJSONTransformer
{
return [MTLJSONAdapter arrayTransformerWithModelClass:[Image class]];
}
@end
@implementation Content
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"type" : @"type",
@"text" : @"text",};
}
@end
@implementation Image
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"src" : @"src",
@"caption" : @"caption",};
}
@end
Then, when I make the call to the service and try to parse the json with the following code, being responseObject the data obtained from server, the data appears nil: 然后,当我调用服务并尝试使用以下代码解析json时,即从服务器获取的数据为responseObject时,数据显示为nil:
for (NSArray *array in [responseObject valueForKey:@"sections"]) {
NSArray *seccionArray = [MTLJSONAdapter modelsOfClass:[Section class] fromJSONArray:array error:nil];
}
I have tried a lot of ways to parse this data well, but the app always crashes or returns nil. 我尝试了很多方法来很好地解析此数据,但是应用程序始终崩溃或返回nil。 I hope you can help me to solve this 我希望你能帮助我解决这个问题
Why can't just one line using NSJSONSerialization
? 为什么不能只使用NSJSONSerialization
?
NSMutableDictionary *yourArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
Then you fetch what you want from your array... 然后,您从阵列中获取所需的内容...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.