In this case, how to map this json to model?

98 Views Asked by At

Here is a json:

{
  query_list: [
    {
      restaurant: {},
      foods: [
        food1: {},
        food2: {}
      ]
    },
    {
      restaurant: {},
      foods: [
        food3: {},
        food4: {}
      ]
    }
  ],
  url: ""
}

I want use Mantle to map it to:

@property NSString *url
@property NSArray<Foods *> *list

the list need to contains all foods. In this case, foods are food1 food2 food3 food4.

So how to get all foods, combine them to a new array which map to the property list

2

There are 2 best solutions below

0
CodeChanger On BEST ANSWER

Based on your JSON formate you have to done below thing to get all food object in single list :

 NSMutableArray * mutArrayFoods = [[NSMutableArray alloc] init];

 for (QueryList *objQueryList in objParseData.queryList) {
      [mutArrayFoods addObjectsFromArray:objQueryList.foods];
 }

Hope this will help to get all foods in single array.

Now as per your request assign this above array into list

list = [mutArrayFoods mutableCopy];
0
Qun Li On

I change your "Pseudo json code" many times. Maybe your thinking like this:

enter image description here

I try my best to create a model called "HF" for helping you: enter image description here

enter image description here