0

如果我有这样的 JSON 数组,

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

和两个模型,如:

@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

股票清单.m

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

+ (NSValueTransformer *)stockLstItemsJSONTransformer {



}

如何解析JSON列表数组属性存储stockListItem的属性?非常感谢!

4

2 回答 2

1

如果您确定索引是,您只需按索引设置它们:

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

否则,您可以使用另一个方法Dictionarylist获取您想要的确切数据,就像:

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

现在:

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

希望这会有所帮助。

于 2015-08-24T06:24:29.290 回答
0

NSJSONSerialization您可以使用的类方法解析 JSON JSONObjectWithData

它会返回一个NSDictionary,然后你可以像这样访问“列表”字段:

NSMutableDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: nil];
stockListObj.stockListItems = parsedJSON[@"list"];
于 2015-08-24T03:47:12.563 回答