Mapping a JSON array of unnamed values with RESTKIT 0.20

半城伤御伤魂 提交于 2019-12-24 01:46:07

问题


When I configure mapping from a REST service returning JSON to a object, I normally do this:

RKObjectMapping *myMapping = [RKObjectMapping mappingForClass:[MyClass class]];
[myMapping addAttributeMappingsFromDictionary:@{@"Address" : @"address", @"City" : @"city"}];

and this works great for JSON with named attributes, but how do I map the following JSON to a object with the property "name"?

["My Value","Some other value","More stuff","Hello World"]

This JSON is just a array of values and has not name/key only values. How do I map this to a object with RESTKIT 0.20?

Thank you
Søren


回答1:


This expression in square brackets is a json array: http://www.json.org. If you look on the syntax tree on the home page, you can consider, every json array is a value of a "variable" with a name. It means your expression has to look like this, to be a valid json:

{ "myArray": ["My Value","Some other value","More stuff","Hello World"] }

and you map it like you always do:

[myMapping addAttributeMappingsFromDictionary:@{@"myArray" : @"myArray"}];

Your parameter MyArray in mapping target class has then a type of NSArray.



来源:https://stackoverflow.com/questions/15379552/mapping-a-json-array-of-unnamed-values-with-restkit-0-20

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!