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

如何在ios中创建以下json字符串?,ios,objective-c,json,Ios,Objective C,Json,如何在没有nsmutable字符串的情况下创建以下格式 { "appointments" : [ { "pincode" : null, "current_status" : "1", "initial_status" : "1", "hospital_id" : "12", "expected_treatment_date" : "18 Nov 2016",

如何在没有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"];

    }
以下代码将在json中转换:

    NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
    NSLog(@"jsonData as string:\n%@", jsonString);
我已成功转换格式,但…我无法通过多个字典

谢谢

请帮帮我

"appointments" : [ {},{} ]
这是一个字典数组,因此您需要将NSDictionary添加到一个数组中,然后需要将该数组转换为JSON并将其传递给服务器

 dicAppointment = [[NSMutableDictionary alloc]init];
 arrAppointment = [[NSMutableArray alloc]init];
现在,在循环的最后一步,您需要在
arrappoims

编辑

在这里,您需要传递数组而不是字典

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

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

Edit2

 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];
}
arrappoint=[[NSMutableArray alloc]init];
对于(int i=0;i
我找到了另一种方法:

NSMutableArray *menuItemNameImage = [NSMutableArray array];
    for (int i = 0 ; i < arrAppointmentData.count ; i++) {
        [menuItemNameImage addObject: @{
                                        @"local_id" : [[arrAppointmentData objectAtIndex:i]valueForKey:@"rowid"]
                                        ,   @"id" :[[arrAppointmentData objectAtIndex:i]valueForKey:@"id"],
                                        @"fullname":[[arrAppointmentData objectAtIndex:i]valueForKey:@"Fullname"],
                                        @"email":[[arrAppointmentData objectAtIndex:i]valueForKey:@"email"],
                                        @"mobileno":[[arrAppointmentData objectAtIndex:i]valueForKey:@"mobileNo"],
                                        @"appt_datetime":[[arrAppointmentData objectAtIndex:i]valueForKey:@"date_time"],
                                        @"address":[[arrAppointmentData objectAtIndex:i]valueForKey:@"address"]
                                        }];
    }

JSON中[]是数组的符号。除非您了解并需要KVC功能,否则不要使用
valueForKey:
/
setValue:forKey:
。50次获取
arrAppointmentData objectAtIndex:i
的效率非常低。能否将字典转换为数组?我在数组中添加了字典…但在我的字典中,有这么多值,我只获取了数组中的最后一个数据请检查我的聊天室检查数组值这两个值是相同的
 NSDictionary *d = @{ @"appointments": menuItemNameImage ,@"lead_followups":arrStatus,@"lead_status":arrLeadStatus };