从CoreData在NSDictionary中制作嵌套的JSON

我不知道标题是否与问题完全一致,但是如果你觉得缺less一些好的东西,编辑问题。
我想发送像JSON这样的特定格式的参数到AFNeworking以便将数据保存在服务器上。
我有一个名为fetchedDXNSArray ,我用CoreData的值加载它作为fetchedDX = [Diagnoses MR_findAllWithPredicate:predicate]; 这是正确的价值,因为我在一个地方使用它的数量,它给了我正确的计数。 这是我准备发送的硬编码NSDictionary:

 NSDictionary *superBillData = @{ @"appointmentID": @"ABC", @"patientID": @"ABC", @"createdBy": @"ABC", @"lastChangedBy": @"ABC", @"Diagnoses":@[ @{ @"Code":@"hello world", @"shortDescription": @"my@you.com" }, @{ @"Code":@"hello 2", @"shortDescription": @"Lorem Ipsum" }, ]}; 

这里的Diagnoses键是发送多个对象的键。 现在,我已经在fetchedDX数组中的这些数据作为核心数据给了我,但我不知道如何使一个dynamic的NSDictionary因为这个数组可以有一个或多个计数。 所以根据我创造的每个价值:

 @{ @"Code":@"hello world", @"shortDescription": @"my@you.com" } 

这是我的实体的图片:
在这里输入图像说明
任何想法如何能够实现?

我假设CodeshortDescription是您的Diagnoses实体的属性。 在Diagnoses.h声明一个实例方法- (NSDictionary *)dictionary; 并在您的Diagnoses.m文件中执行此操作。

 - (NSDictionary *)dictionary { NSArray *keys = @[@"Code", @"codeDescription"]; NSDictionary *dict = [self dictionaryWithValuesForKeys:keys]; return dict; } 

然后,在准备参数字典的地方将您的代码更改为此。

 NSMutableArray *diagnosesArray = [NSMutableArray array]; for (Diagnoses *obejct in fetchedDX) { [diagnosesArray addObject:[obejct dictionary]]; } NSDictionary *superBillData = @{ @"appointmentID": @"ABC", @"patientID": @"ABC", @"createdBy": @"ABC", @"lastChangedBy": @"ABC", @"Diagnoses":diagnosesArray};