[英]How to put the JSON data in to another JSON data in Objective-C?
我正在尝试通过在Objective-C中使用POST发送JSON数据。
我有两个NSDictionary,我想将第一个NSDictionary **(json_dst)**放入第二个NSDictionary **(json)**。
我尝试以下代码,但似乎不正确...
NSString DEVICE_UUID = @"9c88da0c-5362-460b-8647-590a981d912e";
int command = 65536;
NSDictionary *json_dst = [NSDictionary dictionaryWithObjectsAndKeys:@"type", @"Device",@"uuid",DEVICE_UUID, nil];
NSDictionary *json = [NSDictionary dictionaryWithObjectsAndKeys:@"id",@"200",@"dst",json_dst,@"msgType",@"MESSAGE",@"msg",command, nil];
它将在NSDictionary *json = [NSDictionary dictionaryWithObjectsAndKeys:@"id",@"200",@"dst",json_dst,@"msgType",@"MESSAGE",@"msg",command, nil];
崩溃NSDictionary *json = [NSDictionary dictionaryWithObjectsAndKeys:@"id",@"200",@"dst",json_dst,@"msgType",@"MESSAGE",@"msg",command, nil];
。
错误日志是
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid (non-string) key in JSON dictionary'
但是我不知道原因
有人可以教我怎么做吗? 我错过了什么吗?
提前致谢。
首先,您的键和对象顺序错误。
其次,您尝试将原始类型放入字典中(命令是int)。 字典必须包含Objective-C实例,该实例是NSObject的子类。
最后,您的DEVICE_UUID缺少指针星号(*)。
为了使字典更易于使用并保持键值的顺序,我将使用现代的Objective-C文字进行演示。
您需要将其包装在NSNumber中,而不是将其直接放入。 这可以通过将@(command)
内的数字“装箱”来实现,这是文字的另一个功能。
NSString *DEVICE_UUID = @"9c88da0c-5362-460b-8647-590a981d912e";
int command = 65536;
NSDictionary *json_dst = @{@"type":@"Device", @"uuid":DEVICE_UUID};
NSDictionary *json = @{@"id":@"200", @"dst":json_dst, @"msgType":@"MESSAGE", @"msg":@(command)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.