Map parent value to a property in nested dictionary with JSONModel

419 Views Asked by At

I'm not even sure if this is possible but I'd like to map a property on a node to each item in an array which is also a property on the node.

JSON example

"productType" : {
  "name" : "foo",
  "products" : []
}

So I want the value of name to be a property on each product.

I have a productType model set up which has an NSArray on there and everything is mapped by JSONModel. I was thinking there could be a way using the keypath in JSONKeyMapper? But couldn't find anything.

1

There are 1 best solutions below

0
On

You can now do this with JSONModel.

Assume we have the following model:

@class MyModel

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSArray *products;

@end

If the complete JSON doc looks like this:

{
  "productType1": {
    "name": "foo",
    "products": []
  },
  "productType2": {
    "name": "foo",
    "products": []
  },
  "productType3": {
    "name": "foo",
    "products": []
  }
}

then you should use one of the [MyModel dictionaryOfModelsFrom...]; methods.


If it looks like this:

{
  "productTypes": {
    "productType1": {
      "name": "foo",
      "products": []
    },
    "productType2": {
      "name": "foo",
      "products": []
    },
    "productType3": {
      "name": "foo",
      "products": []
    }
  }
}

Then you should use another model like this:

@class MyModelContainer

@property (strong, nonatomic) NSDictionary <MyModel> *productTypes;

@end