• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何在ios中创建以下json字符串?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:48:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

如何在没有 nsmutable 字符串的情况下创建以下格式?

  {
      "appointments" : [
        {
          "pincode" : null,
          "current_status" : "1",
          "initial_status" : "1",
          "hospital_id" : "12",
          "expected_treatment_date" : "18 Nov 2016",
          "appt_comments" : null,
          "lead_followup_by" : "2",
          "speciality_id" : "3",
          "lead_followup_datetime" : "2016-11-17 16:23:17",
          "email" : null,
          "local_id" : 6,
          "mobileno" : "536868868868",
          "enquiry_created_date" : null,
          "id" : "0",
          "enquiry_created_by" : null,
          "state_id" : null,
          "address" : null,
          "fullname" : "googhvvjjc",
          "lead_created_by" : "166",
          "lead_comments" : " H h hh. J",
          "appt_datetime" : null,
          "city_id" : null,
          "other_hospital" : "",
          "cash_credit" : "Cash",
          "create_plan_mail_sent" : "0",
          "interested" : "1",
          "lead_created_date" : "2016-11-17 04:23:24",
          "appt_created_date" : null,
          "appt_created_by" : null
        },
        {
          "pincode" : null,
          "current_status" : "1",
          "initial_status" : "1",
          "hospital_id" : "12",
          "expected_treatment_date" : "18 Nov 2016",
          "appt_comments" : null,
          "lead_followup_by" : "2",
          "speciality_id" : "3",
          "lead_followup_datetime" : "2016-11-17 16:23:17",
          "email" : null,
          "local_id" : 6,
          "mobileno" : "536868868868",
          "enquiry_created_date" : null,
          "id" : "0",
          "enquiry_created_by" : null,
          "state_id" : null,
          "address" : null,
          "fullname" : "googhvvjjc",
          "lead_created_by" : "166",
          "lead_comments" : " H h hh. J",
          "appt_datetime" : null,
          "city_id" : null,
          "other_hospital" : "",
          "cash_credit" : "Cash",
          "create_plan_mail_sent" : "0",
          "interested" : "1",
          "lead_created_date" : "2016-11-17 04:23:24",
          "appt_created_date" : null,
          "appt_created_by" : null
        }
      ],
      "lead_status" : [
        {

        }
      ],
      "lead_followups" : [
        {

        }
      ]
    }

我的代码:

 dicAppointment = [[NSMutableDictionary alloc]init];

    for (int i = 0; i < arrAppointmentData.count; i++)
    {
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey"rowid"] forKey"local_id"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey"id"] forKey"id"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey"Fullname"] forKey"fullname"];
         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey"email"] forKey"email"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey"mobileNo"] forKey"mobileno"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"date_time"] forKey:@"appt_datetime"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"address"] forKey:@"address"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"cityid"] forKey:@"city_id"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"stateid"] forKey:@"state_id"];
         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"pincode"] forKey:@"pincode"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"speciality_id"] forKey:@"speciality_id"];


        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_comments"] forKey:@"lead_comments"];
        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_comment"] forKey:@"appt_comments"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"enquiry_created_date"] forKey:@"enquiry_created_date"];

                  [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"enquiry_created_by"] forKey:@"enquiry_created_by"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"intrested"] forKey:@"interested"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_follow_by"] forKey:@"lead_followup_by"];

       [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_follow_datetime"] forKey:@"lead_followup_datetime"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_created_date"] forKey:@"lead_created_date"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_created_by"] forKey:@"lead_created_by"];


         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_created_date"] forKey:@"appt_created_date"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_created_by"] forKey:@"appt_created_by"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"initial_status"] forKey:@"initial_status"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"current_status"] forKey:@"current_status"];

           [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"hospital_id"] forKey:@"hospital_id"];

        [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"cashcredit"] forKey:@"cash_credit"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"expectdate"] forKey:@"expected_treatment_date"];

           [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"other_hospital"] forKey:@"other_hospital"];

         [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"create_plan_mail_sent"] forKey:@"create_plan_mail_sent"];

    }

三个字典都一样:

现在将字典合并到以下字典上

  NSDictionary *d = @{
                    @"appointments": @[ dicAppointment
                            ],@"lead_followups":@[dicleadStatuss],@"lead_status":@[dicleadStatus]

                    };

以下代码在 json 中隐藏:

    NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
    NSLog(@"jsonData as string:\n%@", jsonString);

我已成功转换格式,但是...我无法传递多个字典。

谢谢

请帮帮我



Best Answer-推荐答案


这里,

"appointments" : [ {},{} ]

这是字典数组,因此您需要将 NSDictionary 添加到数组中,然后需要将该数组转换为 JSON 并将其传递给您的服务器。

 dicAppointment = [[NSMutableDictionary alloc]init];
 arrAppointment = [[NSMutableArray alloc]init];

现在在循环的最后,您需要在 arrAppointment

中添加 dicAppointment

编辑

这里你需要传递你的数组来代替字典

 NSDictionary *d = @{ @"appointments":arrAppointment ,@"lead_followups":arrStatus,@"lead_status":arrLeadStatus };

试试这个

编辑2

 arrAppointment = [[NSMutableArray alloc]init];

for (int i = 0; i < arrAppointmentData.count; i++)
{
dicAppointment = [[NSMutableDictionary alloc]init];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"rowid"] forKey:@"local_id"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"id"] forKey:@"id"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"Fullname"] forKey:@"fullname"];
     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"email"] forKey:@"email"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"mobileNo"] forKey:@"mobileno"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"date_time"] forKey:@"appt_datetime"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"address"] forKey:@"address"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"cityid"] forKey:@"city_id"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"stateid"] forKey:@"state_id"];
     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"pincode"] forKey:@"pincode"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"speciality_id"] forKey:@"speciality_id"];


    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_comments"] forKey:@"lead_comments"];
    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_comment"] forKey:@"appt_comments"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"enquiry_created_date"] forKey:@"enquiry_created_date"];

              [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"enquiry_created_by"] forKey:@"enquiry_created_by"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"intrested"] forKey:@"interested"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_follow_by"] forKey:@"lead_followup_by"];

   [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_follow_datetime"] forKey:@"lead_followup_datetime"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_created_date"] forKey:@"lead_created_date"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"lead_created_by"] forKey:@"lead_created_by"];


     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_created_date"] forKey:@"appt_created_date"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"appointment_created_by"] forKey:@"appt_created_by"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"initial_status"] forKey:@"initial_status"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"current_status"] forKey:@"current_status"];

       [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"hospital_id"] forKey:@"hospital_id"];

    [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"cashcredit"] forKey:@"cash_credit"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"expectdate"] forKey:@"expected_treatment_date"];

       [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"other_hospital"] forKey:@"other_hospital"];

     [dicAppointment setValue:[[arrAppointmentData objectAtIndex:i]valueForKey:@"create_plan_mail_sent"] forKey:@"create_plan_mail_sent"];
     [arrAppointment addObject: dicAppointment];
}

关于ios - 如何在ios中创建以下json字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653365/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap