使用 RESTKIT 0.20 映射未命名值的 JSON 数组 [英] Mapping a JSON array of unnamed values with RESTKIT 0.20
问题描述
当我配置从 REST 服务返回 JSON 到对象的映射时,我通常这样做:
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"}];
这对于具有命名属性的 JSON 非常有用,但是如何将以下 JSON 映射到具有属性name"的对象?
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"]
这个 JSON 只是一个值数组,没有名称/键值.如何使用 RESTKIT 0.20 将其映射到对象?
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?
谢谢
索伦
推荐答案
方括号中的这个表达式是一个 json 数组:http://www.json.org.如果查看主页上的语法树,可以考虑,每个json数组都是一个带有名称的变量"的值.这意味着您的表达式必须如下所示,才能成为有效的 json:
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"}];
您在映射目标类中的参数MyArray
则具有NSArray
的类型.
Your parameter MyArray
in mapping target class has then a type of NSArray
.
这篇关于使用 RESTKIT 0.20 映射未命名值的 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!