从CoreData在NSDictionary中制作嵌套的JSON [英] Making Nested JSON in NSDictionary from CoreData

查看:112
本文介绍了从CoreData在NSDictionary中制作嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道标题是否与问题完全吻合,但是如果您觉得缺少标题,请编辑问题。
我想以 JSON之类的特定格式发送参数 AFNeworking 以便将数据保存在服务器上。

我有一个 NSArray (其中名为 fetchedDX ),然后使用 fetchedDX = [Diagnoses MR_findAllWithPredicate:predicate]从CoreData加载它; 在我使用它时获得正确的值,它只在一个地方计数,并且给了我正确的计数。这是我准备发送的硬代码NSDictionary:

I don't know if the title goes exactly with the question or not but if you feel it's missing something kindly edit the question.
I want to send parameter in specific format like JSON to AFNeworking in order to save the data on server.
I've an NSArray in which is named fetchedDX and I'm loading it with values from CoreData as fetchedDX = [Diagnoses MR_findAllWithPredicate:predicate]; which is getting the right values as I'm using it's count at one place and it is giving me the right count. This is the hardcode NSDictionary I'm preparing to send:

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 数组中,因为核心数据给了我,但我不知道如何制作动态的 NSDictionary 因为此数组可以具有一个或多个计数。因此,基于每个值,我必须创建它:

Here the Diagnoses key is the one sending multiple objects. Now I've this data in fetchedDX array as core data gave me but I don't know how to make a dynamic NSDictionary because this array can have a count of one or more than one. So based on every value I've to create this:

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

这是我实体的图片:



任何想法如何实现?

Here is the picture of my entity:

Any ideas how can this be achieved?

推荐答案

我假设代码 shortDescription 是您的诊断实体的属性。在 Diagnoses.h 中声明实例方法-(NSDictionary *)dictionary; 并在您的 Diagnoses.m 文件。

I assume that Code and shortDescription are the attributes of your Diagnoses entity. In the Diagnoses.h declare an instance method - (NSDictionary *)dictionary; and do this in your Diagnoses.m file.

- (NSDictionary *)dictionary {

    NSArray *keys = @[@"Code", @"codeDescription"];
    NSDictionary *dict = [self dictionaryWithValuesForKeys:keys];

    return dict;
}

然后在准备参数字典的位置将代码更改为此。

Then where you prepare the parameters dictionary change your code to this.

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};

这篇关于从CoreData在NSDictionary中制作嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆