2016-11-17 3 views
2

wie das folgende Format ohne nsmutable Zeichenfolge zu erstellen?Wie erstellt man folgende JSON-Zeichenfolge in ios?

{ 
     "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" : [ 
     { 

     } 
     ] 
    } 

Mein Code:

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"]; 

    } 

Alle drei Wörterbuch sind gleich:

jetzt die dictionry auf folgenden Wörterbuch

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

        }; 

in json verdeckte sind folgenden Code verschmelzen verschmelzen:

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

Ich habe Format erfolgreich konvertiert, aber ... Ich kann mehrere Wörterbücher nicht übergeben.

Dank

Bitte helfen Sie mir

+0

in JSON [] ist das Zeichen des Arrays. – vaibhav

+0

Verwenden Sie 'valueForKey:'/'setValue: forKey:' sofern Sie die KVC-Funktionalität nicht verstehen und benötigen. Und 50 mal bekommen 'arrAppointmentData objectAtIndex: i's ist sehr sehr sehr ineffizient. – vadian

Antwort

2

Hier

"appointments" : [ {},{} ] 

dies Array von Wörterbücher, so müssen Sie Ihre NSDictionary einem Array hinzuzufügen und müssen dann dieses Array in JSON konvertieren und gib es an deinen Server weiter.

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

jetzt in der letzten Ihrer Schleife müssen Sie dicAppointment in arrAppointment

bearbeiten

Hier können Sie Ihr Array anstelle von Wörterbuch

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

passieren müssen hinzufügen Versuchen Sie diese

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]; 
} 
+0

können Sie bitte Wörterbuch in Array konvertieren –

+0

nur letzte Wörterbuchdaten in meinem Array –

+0

Ich habe Wörterbuch in meinem Array hinzugefügt ... aber in meinem Wörterbuch so viele Werte habe ich nur letzte Daten im Array –

0

fand ich eine andere Art und Weise, dies zu tun:

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

Fahren Sie mit Array

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