映射父对象内的对象数组 - Mantle iOS

Jef*_*eff 7 objective-c ios github-mantle

我有一些看起来像这样的json数据:

    {
       items: [
                { // object 1
                  aProperty: "aValue",
                  anotherProperty: "anotherValue",
                  anObjectProperty: {}
                 },
                 { //object 2
                  aProperty: "aValue",
                  anotherProperty: "anotherValue",
                  anObjectProperty: {}
                 } 
       ]
    }
Run Code Online (Sandbox Code Playgroud)

我想使用Mantle将这个json映射到两个对象的数组中.

这将如下所示:

    @interface MyObject : MTLModel <MTLJSONSerializing>

    @property (nonatomic, strong) NSString *myProperty;
    @property (nonatomic, strong) NSString *anotherProperty;
    @property (nonatomic, strong) NSObject *anObject;

    @end

    @implementation MyObject

    + (NSDictionary *)JSONKeyPathsByPropertyKey {
        return @{
                 @"myProperty": @"myProperty",
                 @"anotherProperty" : @"anotherProperty",
                 @"anObject": @"anObject"
                 };
    }

    @end
Run Code Online (Sandbox Code Playgroud)

但是,这需要我去json中找到"items"键,然后解析该键内部的内容.

相反,我希望Mantle只为我绘制整个对象.所以,我提出了这个解决方案:

    @interface MyObjects : MTLModel <MTLJSONSerializing>

    @property (nonatomic) NSArray *items;

    @end

    @implementation MyObjects

    + (NSDictionary *)JSONKeyPathsByPropertyKey {
        return @{
                 @"items": @"items"
                 };
    }

    + (NSValueTransformer *)itemsJSONTransformer
    {
        return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:[MyObject class]];
    }

    @end
Run Code Online (Sandbox Code Playgroud)

当所有设置完成后,这将给我留下如下内容

NSArray *myArrayOfObjects = (MyObjects*)myobjects.items;
Run Code Online (Sandbox Code Playgroud)

这一切都很棒,但我相信创建一个"MyObjects"类,只是作为一个MyObject数组的占位符是过度的.有更好的解决方案吗?理想情况下,我正在寻找一个地幔设置(或者比创建两个类只是为了获得一个对象的数组更容易)来为我处理根"item"键,这样当它解析时,它就会出现一个包含2个对象的数组.

谢谢!

小智 3

在我们在MTLJSONAdapter中映射Jsonobject的地方

\n\n

如果 json 是对象类型,则该对象应映射为

\n\n
object = [MTLJSONAdapter modelOfClass:responseType fromJSONDictionary:jsonObject error:&error];\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果 json 是数组类型,则该对象应映射为

\n\n
object = [MTLJSONAdapter modelsOfClass:responseType fromJSONArray:jsonObject error:&error];\n\n[\n       {\n                { // object 1\n                  aProperty: "aValue",\n                  anotherProperty: "anotherValue",\n                  anObjectProperty: {}\n                 },\n                 { //object 2\n                  aProperty: "aValue",\n                  anotherProperty: "anotherValue",\n                  anObjectProperty: {}\n                 } \n       }\n       {\n                { // object 1\n                  aProperty: \xe2\x80\x9cbValue",\n                  anotherProperty: "anotherValue",\n                  anObjectProperty: {}\n                 },\n                 { //object 2\n                  aProperty: \xe2\x80\x9cbValue",\n                  anotherProperty: "anotherValue",\n                  anObjectProperty: {}\n                 } \n       }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果是这种情况,可以这样映射

\n\n
object = [MTLJSONAdapter modelsOfClass:responseType fromJSONArray:jsonObject error:&error];\n
Run Code Online (Sandbox Code Playgroud)\n