1

I'm a bit new to RKObjectMapping, and I'm having some trouble figuring this out.

I have a class which has an array of objects called myObjects. This type of object already has an object mapping in its class. I want to map to the following kind of JSON object:

{
  "myObjects": [...]
}

where the JSON array contains JSON objects determined by myObjects and the object mapping I am already given.

How would I go about making an object mapping to do this?

ferson2020
  • 3,015
  • 3
  • 18
  • 26

2 Answers2

0

I am sorry I misunderstood your question. If I were to do this I would use JSONKit. This allows you to create NSDictionarys and NSArrays from JSON objects and also serialize JSON objects from them.

If you include the .h/.m in your project (This is also included automatically in RestKit under Vendor > JSONKit) you can import JSONKit.h and the then just call:

[myArray JSONString];

This returns you an NSString of your serialized JSON Object. There are also some other, more thorough methods that you may want to take a look at.


Original, incorrect answer:

I think this is what you are looking for:

RKObjectMapping *visualsMapping = [RKObjectMapping mappingForClass:[Visuals class]];
[carMapping mapKeyPath:@"Color" toRelationship:@"color"];
[carMapping mapKeyPath:@"Damage" toRelationship:@"damage"];
...

carMapping = [RKObjectMapping mappingForClass:[Car class]];
[carMapping mapKeyPath:@"Model" toRelationship:@"model"];
[carMapping mapKeyPath:@"Visuals" toRelationship:@"visuals" withMapping:visualsMapping];
...
[carMapping setRootKeyPath:@"myObjects"];

Here you can make against custom objects that are contained inside of other custom objects and also map attributes directly without specifying a mapping to go with it.

Firo
  • 15,448
  • 3
  • 54
  • 74
  • There is nothing here referencing the object mapping I have for myObjects. – ferson2020 Jan 30 '13 at 20:52
  • Updated answer, hope that helps, if not can you give me a bit more information? – Firo Jan 30 '13 at 22:18
  • The idea is I would like to serialise into a JSON object like this: { "visuals": [{ "color": "blue", "damage": "none" }, { "color": "red", "damage": "heavy"}] } – ferson2020 Jan 31 '13 at 13:45
  • Oh, you have Objc-C objects and want to create JSON objects? I was assuming the other way around. Is this correct? – Firo Jan 31 '13 at 17:18
  • Yes, this is correct. Unfortunately, it seems to be very hard to find examples of mapping Obj-C objects to JSON objects and not the other way around. – ferson2020 Jan 31 '13 at 17:19
  • Updated answer, hopefully this is what you were looking for. – Firo Jan 31 '13 at 17:28
0

After a lot of annyoing fiddling, I figured it out:

If I want to have a named JSON array nested inside a JSON object, I have to define an object mapping from the array to the JSON array, then define a mapping for the whole JSON object with empty key path and relationship that uses the first object mapping.

For example:

RKObjectMapping* visualsMapping = [RKObjectMapping serializationMapping]'
[visualsMapping mapKeyPath:@"visuals" toRelationship:@"Visuals" withMapping:[Visual serializaitonMapping];

RKobjectMapping* serializaionMapping = [RkObjectMapping serializationMapping];
[serializaionMapping mapKeyPath:@"" toRelationship:@"" visualsMapping];

Then the object mapping I want is serializationMapping.

ferson2020
  • 3,015
  • 3
  • 18
  • 26