0

If I have a JSON array like this,

{
  "list": [
    "javascript", 
    "stockFields",
    "stockLists"
  ]
}

and two models like:

@interface stockList : MTLModel <MTLJSONSerializing>

@property(nonatomic, copy, readonly) NSArray *stockListItems;

@end

@interface stockListItem : MTLModel

@property(nonatomic, copy, readonly) NSString *javascript;

@property(nonatomic, copy, readonly) NSString *stockFields;

@property(nonatomic, copy, readonly) NSString *stockLists;

@end

stockList.m

+ (NSDictionary*)JSONKeyPathsByPropertyKey {
    return @{
             @"stockListItems":@"list",
             };
}

+ (NSValueTransformer *)stockLstItemsJSONTransformer {



}

How to parse JSON list array attribute storage stockListItem's properyty ?thanks a lot!

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
SimonKira
  • 91
  • 1
  • 4
  • 13

2 Answers2

1

You just set them by index if you are sure the indexes are:

    javascript = stockListItems[0];
    stockFields = stockListItems[1];
    stockLists = stockListItems[2];

Otherwise, you could have another Dictionary in list to get exact data you want, just like:

{
  "list": [
    item1: "javascript", 
    item2: "stockFields",
    item3: "stockLists"
  ]
}

and now:

javascript = [list objectForKey:@"item1"];
stockFields = [list objectForKey:@"item2"];
stockLists = [list objectForKey:@"item3"];

Hope this could help.

Nghia Luong
  • 790
  • 1
  • 6
  • 11
0

You can parse JSON with NSJSONSerialization's class method JSONObjectWithData.

It'll return a NSDictionary and then you can access the "list" field like so:

NSMutableDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: nil];
stockListObj.stockListItems = parsedJSON[@"list"];
hola
  • 3,150
  • 13
  • 21
  • sorry~i want to storage list array with stockListItem's property!!pls tell me again!thanks~ How can use + (NSValueTransformer *)stockLstItemsJSONTransformer? – SimonKira Aug 24 '15 at 05:39