RestKit JSON 嵌套方括号
对于我的 JSON,我希望获得以下格式:
"fromTrigger":{
"id":1,
"fieldId":2,
"type":"FROM"
}
但是通过我的映射,我得到以下格式:
"fromTrigger":[
{
"id":1,
"fieldId":2,
"type":"FROM"
}
]
如您所见,我在其中添加了额外的方括号“[ ]”第二个。
我有以下映射:
RKObjectMapping* fromTriggerMapping = [RKObjectMapping mappingForClass:[ActianTriggers class]];
[fromTriggerMapping mapKeyPath:@"id" toAttribute:@"triggerId"];
[fromTriggerMapping mapKeyPath:@"fieldId" toAttribute:@"fieldId"];
[fromTriggerMapping mapKeyPath:@"type" toAttribute:@"type"];
[fromTriggerMapping mapKeyPath:@"filters" toRelationship:@"filters" withMapping:triggerMapping serialize:YES];
[[RKObjectManager sharedManager].mappingProvider setMapping:fromTriggerMapping forKeyPath:@"fromTrigger"];
更新: 感谢您的快速回复,我的问题是如何让“FromTrigger”块的 JSON 只包含花括号而不包含方括号。从上面的两个 JSON 我想实现第一个的格式。我需要这个,因为否则 Web 服务会抛出错误。
ActianTriggers类(以下是头文件,.m只综合了这些变量):
@interface ActianTriggers : NSObject
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSNumber *triggerID;
@property (nonatomic, retain) NSNumber *fieldId;
@property (nonatomic, strong) NSArray *filters;
@end
For my JSON I want to get the following format:
"fromTrigger":{
"id":1,
"fieldId":2,
"type":"FROM"
}
But with my mapping I get the following:
"fromTrigger":[
{
"id":1,
"fieldId":2,
"type":"FROM"
}
]
As you can see, I have the extra square braces "[ ]" in the second one.
I have the following mapping:
RKObjectMapping* fromTriggerMapping = [RKObjectMapping mappingForClass:[ActianTriggers class]];
[fromTriggerMapping mapKeyPath:@"id" toAttribute:@"triggerId"];
[fromTriggerMapping mapKeyPath:@"fieldId" toAttribute:@"fieldId"];
[fromTriggerMapping mapKeyPath:@"type" toAttribute:@"type"];
[fromTriggerMapping mapKeyPath:@"filters" toRelationship:@"filters" withMapping:triggerMapping serialize:YES];
[[RKObjectManager sharedManager].mappingProvider setMapping:fromTriggerMapping forKeyPath:@"fromTrigger"];
UPDATE:
Thank you for the quick responses, my question is that how can I have my JSON for the "FromTrigger" block to only have curly braces and not square braces also. From the two JSONs above I want to achieve the format of the first one. I require this because other wise the web service throws an error.
ActianTriggers Class (Following is the header file, the .m only synthesizes these variables):
@interface ActianTriggers : NSObject
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSNumber *triggerID;
@property (nonatomic, retain) NSNumber *fieldId;
@property (nonatomic, strong) NSArray *filters;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个实例中,
fromTrigger
是一个字典,在第二个实例中,fromTrigger
是一个数组或一个项目,并且该项目是一个字典。请注意,在 JSON 中,
字典
是一个对象
。什么是ActianTriggers?映射自省对象以确定要进行的映射类型。
来自
RKObjectMapping
文档(粗体是我的) :1. 属性的 keyPath。定义应转换在 keyPath 处找到的值并将其分配给属性指定的属性。要执行的转换是通过在运行时检查目标属性的类型来确定的。
请参阅JSON 简介。
In the first instance
fromTrigger
is a dictionary, in the second instancefromTrigger
is an array or one item and that item is a dictionary.Note that in JSON parlance a
dictionary
is anobject
.What is
ActianTriggers
? The mapping introspects the object to determine the kind of mapping to make.From the
RKObjectMapping
documentation (the bold is mine):1. keyPath to attribute. Defines that the value found at the keyPath should be transformed and assigned to the property specified by the attribute. The transformation to be performed is determined by inspecting the type of the target property at runtime.
See Introducing JSON.