RestKit - 对象映射!这里出了什么问题?

发布于 2024-12-11 00:01:39 字数 3308 浏览 2 评论 0原文

我有来自服务器的 JSON 响应。 从 RestKit 日志中获取的数据如下所示:

sourceObject: ( 
    { 
    place =         { 
        "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5846738815"; 
            longtitude = "55.6815948486"; 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:33:44"; 
    }; 
}, 
    { 
    place =         { 
        "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5720376968"; 
            longtitude = "55.6785774231"; 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:37:08"; 
    }; 
}, 
    { 
    Timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
} )and targetObject: (null) 

数据映射到 3 个对象:

Place、Placeinfo 和 LatestDBUpdate。

问题是:

在上面的 JSON 响应中,有 2 个地点对象和 2 个嵌套的 PlaceInfo 对象,以一对多关系连接。

还有一个与 Timestamps 关键字相关的 LastDBUpdate 对象。

Restkit会映射3个Place对象,其中一个全部为NULL。 其他 2 个 Place 对象已正确映射,关系也正确。 它当然应该只映射 2 个 Place 对象!

LastDBUpdate 映射也正确映射时间戳。

我已经确定,如果我删除 JSON 响应的时间戳部分,则映射是正确的,只有 2 个位置对象。

但我需要时间戳部分! 我不知道如何解决这个问题!任何想法 - 我真的可以使用一些输入,因为我已经花了几个小时在这上面!

以下是使用的映射设置:

//Place mapping 
if (!self.placeManagedObject){ 
    self.placeManagedObject = [RKManagedObjectMapping mappingForClass:[Place class]]; 
    self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
    self.placeManagedObject.setDefaultValueForMissingAttributes = YES; 
    [self.placeManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [self.placeManagedObject mapKeyPath:@"timestamp" toAttribute:@"timestamp"]; 
} 
//PlaceInformation mapping 
if (!self.placeInfoManagedObject){ 
    self.placeInfoManagedObject = [RKManagedObjectMapping mappingForClass:[PlaceInfo class]]; 
    self.placeInfoManagedObject.primaryKeyAttribute = @"UUID";self.placeInfoManagedObject.setDefaultValueForMissingAttributes = YES; 
    [placeInfoManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [placeInfoManagedObject mapKeyPath:@"longtitude" toAttribute:@"longtitude"]; 
    [placeInfoManagedObject mapKeyPath:@"latitude" toAttribute:@"latitude"]; 
} 
//latestDBUpdate timestamp mapping 
if (!self.latestDBUpdateManagedObject){ 
    self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]]; 
    self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update"; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"toAttribute:@"latest_update"]; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"uuid" toAttribute:@"uuid"]; 
} 
//Set mapping relations 
[self.placeManagedObject mapRelationship:@"placeinfo" withMapping:self.placeInfoManagedObject]; 
// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"]; 

I have a JSON response from my server.
The data, taken from the RestKit log looks like this:

sourceObject: ( 
    { 
    place =         { 
        "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5846738815"; 
            longtitude = "55.6815948486"; 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:33:44"; 
    }; 
}, 
    { 
    place =         { 
        "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5720376968"; 
            longtitude = "55.6785774231"; 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:37:08"; 
    }; 
}, 
    { 
    Timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
} )and targetObject: (null) 

The data is mapped into 3 objects:

Place, Placeinfo and LatestDBUpdate.

The problem is:

In the above JSON response there are 2 place objects and 2 nested PlaceInfo objects,connected with a One To Many relationship.

There is also a LatestDBUpdate object that relates to the Timestamps keyword.

Restkit will map 3 Place objects, and one will be all NULL.
The 2 other Place objects are mapped correctly and relations are also correct.
It should of course only map 2 Place objects!

The LatestDBUpdate mapping s also mapping the Timestamps correct.

I have determined, that if i remove the Timestamps part of the JSON respons, then the mapping is correct, with only 2 place objects.

But i need the Timestamps part to be there!
I have no idea how to fix this! any ideas - I could really use some input, since i have used hours upon hours on this!

Here are the mapping setup used:

//Place mapping 
if (!self.placeManagedObject){ 
    self.placeManagedObject = [RKManagedObjectMapping mappingForClass:[Place class]]; 
    self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
    self.placeManagedObject.setDefaultValueForMissingAttributes = YES; 
    [self.placeManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [self.placeManagedObject mapKeyPath:@"timestamp" toAttribute:@"timestamp"]; 
} 
//PlaceInformation mapping 
if (!self.placeInfoManagedObject){ 
    self.placeInfoManagedObject = [RKManagedObjectMapping mappingForClass:[PlaceInfo class]]; 
    self.placeInfoManagedObject.primaryKeyAttribute = @"UUID";self.placeInfoManagedObject.setDefaultValueForMissingAttributes = YES; 
    [placeInfoManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [placeInfoManagedObject mapKeyPath:@"longtitude" toAttribute:@"longtitude"]; 
    [placeInfoManagedObject mapKeyPath:@"latitude" toAttribute:@"latitude"]; 
} 
//latestDBUpdate timestamp mapping 
if (!self.latestDBUpdateManagedObject){ 
    self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]]; 
    self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update"; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"toAttribute:@"latest_update"]; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"uuid" toAttribute:@"uuid"]; 
} 
//Set mapping relations 
[self.placeManagedObject mapRelationship:@"placeinfo" withMapping:self.placeInfoManagedObject]; 
// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"]; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-12-18 00:01:39

又回来了,带着我自己的问题的答案……看来这里使用Restkit的会员并不多!

我的解决方案,也许不漂亮,但唯一有效的,没有
删除 JSON 响应的时间戳部分:

我按以下方式修改了 JSON 响应:

{ 
placeList =     ( 
            { 
        place =             { 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5846738815"; 
                longtitude = "55.6815948486"; 
                "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:33:44"; 
        }; 
    }, 
            { 
        place =             { 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5720376968"; 
                longtitude = "55.6785774231"; 
                "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:37:08"; 
        }; 
    } 
); 
updateData =     { 
    timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
};} 

为地点添加一种主键 -> placeList 和 updateData 用于时间戳。

我还需要更改提供程序的密钥路径以反映对 JSON 输入的更改:

// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"placeList.place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"updateData.timestamps"]; 

所以它解决了这个问题。不再有 NULL 对象,映射,即使你我不这样做
非常理解为什么对 JSON 的更改会造成如此大的影响
映射的差异。!

Back again, with an answer to my own problem... Seems there are not many members here that use Restkit!

My solution, maybe not pretty, but the only that worked, without
removing the Timestamps part of the JSON response:

I modified the JSON response in the following way:

{ 
placeList =     ( 
            { 
        place =             { 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5846738815"; 
                longtitude = "55.6815948486"; 
                "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:33:44"; 
        }; 
    }, 
            { 
        place =             { 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5720376968"; 
                longtitude = "55.6785774231"; 
                "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:37:08"; 
        }; 
    } 
); 
updateData =     { 
    timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
};} 

Adding a kind of primary key for places -> placeList and updateData for timestamps.

I also needed to change the keypath for the provider to reflect the change to the JSON input:

// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"placeList.place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"updateData.timestamps"]; 

So it solved it. No more NULL objects, mapped, even thou i do not
quite understand why the change to the JSON makes such a big
difference in the mapping.!

尸血腥色 2024-12-18 00:01:39

首先,如果您在提出这样的问题时发布有效的 JSON 会更有帮助 - 您可能会得到更多回复。

其次,第一次尝试的主要问题是将 Timestamps 对象分组为 sourceObject 数组的一部分。您可能会通过以下方式得到您想要的东西:

{
    "sourceObject": [
        {
            "place": {
                "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5846738815", 
                    "longtitude": "55.6815948486", 
                    "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:33:44"
            }
        }, 
        {
            "place": {
                "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5720376968", 
                    "longtitude": "55.6785774231", 
                    "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:37:08"
            }
        }
    ],
    "Timestamps": {
        "latest_update": "2011-10-18 17:12:09", 
        "uuid": "8c6fb842-f99b-11e0-aba9-9415ab1a8034"
    }
}

First, it would be more helpful if you posted valid JSON when asking a question like this -- you'd likely get more responses.

Second, the primary issue in your first attempt is the grouping of the Timestamps object as part of the sourceObject array. You would've likely gotten what you want with the following instead:

{
    "sourceObject": [
        {
            "place": {
                "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5846738815", 
                    "longtitude": "55.6815948486", 
                    "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:33:44"
            }
        }, 
        {
            "place": {
                "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5720376968", 
                    "longtitude": "55.6785774231", 
                    "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:37:08"
            }
        }
    ],
    "Timestamps": {
        "latest_update": "2011-10-18 17:12:09", 
        "uuid": "8c6fb842-f99b-11e0-aba9-9415ab1a8034"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文