문제

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"]; 
도움이 되었습니까?

해결책

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.!

다른 팁

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"
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top