RestKit JSON 嵌套方括号

发布于 2024-12-28 12:02:19 字数 1323 浏览 0 评论 0原文

对于我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2025-01-04 12:02:19

在第一个实例中,fromTrigger 是一个字典,在第二个实例中,fromTrigger 是一个数组或一个项目,并且该项目是一个字典。

请注意,在 JSON 中,字典 是一个对象

什么是ActianTriggers?映射自省对象以确定要进行的映射类型。

来自RKObjectMapping文档(粗体是我的) :
1. 属性的 keyPath。定义应转换在 keyPath 处找到的值并将其分配给属性指定的属性。要执行的转换是通过在运行时检查目标属性的类型来确定的。

请参阅JSON 简介

In the first instance fromTrigger is a dictionary, in the second instance fromTrigger is an array or one item and that item is a dictionary.

Note that in JSON parlance a dictionary is an object.

What is ActianTriggers? The mapping introspects the object to determine the kind of mapping to make.

From the RKObjectMappingdocumentation (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文