[英]Objective-C Mantle, deserializing JSON Dictionary
I'm trying to figure out how to deserialize an object that contains a "dictionary" that is not a full object. 我试图弄清楚如何反序列化包含不是完整对象的“字典”的对象。
For example, in our app, we have a bunch of JSON objects that we're deserializing from JSON via Mantle. 例如,在我们的应用程序中,我们有一堆通过Mantle从JSON反序列化的JSON对象。 A simple model might look like: 一个简单的模型可能看起来像:
@interface Artist : MTLModel<MTLJSONSerializing>
@property (nonatomic, strong, nonnull) NSString *name;
@property (nonatomic, strong, nullable) NSURL *image;
@end
in a collection class, we might have something like this: 在集合类中,我们可能会有类似这样的内容:
@interface SomeCollection : MTLModel<MTLJSONSerializing>
@property (nonatomic, strong, nonnull) NSString *title;
@property (nonatomic, strong, nonnull) NSArray<Artist *> *listOfArtists;
@end
and an associated .m
would have: 关联的.m
将具有:
+ (NSValueTransformer *)listOfArtistsJSONTransformer {
return [MTLJSONAdapter arrayTransformerWithModelClass:[Artist class]];
}
Everything is good here. 这里一切都很好。
For example, if the JSON looks like: 例如,如果JSON看起来像:
{
"title": "my collection with an array",
"listOfArtists": [
{
"name": "Some Artist",
"image": "http://www.google.com"
},
{
"name": "Another Artist",
"image": "http://www.artists.com"
},
{
"name": "Jane Painter",
"image": "http://www.jpainter.com"
},
]
}
The object deserializes just as we like (where the listOfArtists
property contains an array of Artist *
. 该对象反序列化的方式与我们喜欢的一样(其中listOfArtists
属性包含一个Artist *
数组。
However, I'm trying to figure out the incantation if we have a different collection that has: 但是,如果我们有另一个具有以下内容的集合,我想弄清楚这个咒语:
@interface SomeOtherCollection : MTLModel<MTLJSONSerializing>
@property (nonatomic, strong, nonnull) NSString *title;
@property (nonatomic, strong, nonnull) NSDictionary<NSString *, Artist *> *dictionaryOfArtists;
@end
and a JSON file that might look like 和一个可能看起来像的JSON文件
{
"title": "my collection with a dictionary",
"dictionaryOfArtists": {
"139380bf-29ef-4cfc-95af-aa00f78f15f6": {
"name": "Some Artist",
"image": "http://www.google.com"
},
"4cdbc728-13e7-49c8-b45e-32ff0650ca67": {
"name": "Another Artist",
"image": "http://www.artists.com"
},
"2f2ec6f9-3af1-4789-b5de-399e14902ea8": {
"name": "Jane Painter",
"image": "http://www.jpainter.com"
},
}
}
What would the listOfArtistsJSONTransformer
method look like? listOfArtistsJSONTransformer
方法是什么样的?
Thanks. 谢谢。
I don't know much (anything) about Mantle, but it looks like you can use... 我对Mantle不太了解,但是看起来您可以使用...
+ (NSValueTransformer *)assigneeJSONTransformer {
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[Artist class]];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.