ios - 如何在ios中创建以下json字符串?
<p><p>如何在没有 nsmutable 字符串的情况下创建以下格式?</p>
<pre><code>{
"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" : [
{
}
]
}
</code></pre>
<p>我的代码:</p>
<pre><code> dicAppointment = [init];
for (int i = 0; i < arrAppointmentData.count; i++)
{
valueForKey:@"rowid"] forKey:@"local_id"];
valueForKey:@"id"] forKey:@"id"];
valueForKey:@"Fullname"] forKey:@"fullname"];
valueForKey:@"email"] forKey:@"email"];
valueForKey:@"mobileNo"] forKey:@"mobileno"];
valueForKey:@"date_time"] forKey:@"appt_datetime"];
valueForKey:@"address"] forKey:@"address"];
valueForKey:@"cityid"] forKey:@"city_id"];
valueForKey:@"stateid"] forKey:@"state_id"];
valueForKey:@"pincode"] forKey:@"pincode"];
valueForKey:@"speciality_id"] forKey:@"speciality_id"];
valueForKey:@"lead_comments"] forKey:@"lead_comments"];
valueForKey:@"appointment_comment"] forKey:@"appt_comments"];
valueForKey:@"enquiry_created_date"] forKey:@"enquiry_created_date"];
valueForKey:@"enquiry_created_by"] forKey:@"enquiry_created_by"];
valueForKey:@"intrested"] forKey:@"interested"];
valueForKey:@"lead_follow_by"] forKey:@"lead_followup_by"];
valueForKey:@"lead_follow_datetime"] forKey:@"lead_followup_datetime"];
valueForKey:@"lead_created_date"] forKey:@"lead_created_date"];
valueForKey:@"lead_created_by"] forKey:@"lead_created_by"];
valueForKey:@"appointment_created_date"] forKey:@"appt_created_date"];
valueForKey:@"appointment_created_by"] forKey:@"appt_created_by"];
valueForKey:@"initial_status"] forKey:@"initial_status"];
valueForKey:@"current_status"] forKey:@"current_status"];
valueForKey:@"hospital_id"] forKey:@"hospital_id"];
valueForKey:@"cashcredit"] forKey:@"cash_credit"];
valueForKey:@"expectdate"] forKey:@"expected_treatment_date"];
valueForKey:@"other_hospital"] forKey:@"other_hospital"];
valueForKey:@"create_plan_mail_sent"] forKey:@"create_plan_mail_sent"];
}
</code></pre>
<p>三个字典都一样:</p>
<p>现在将字典合并到以下字典上</p>
<pre><code>NSDictionary *d = @{
@"appointments": @[ dicAppointment
],@"lead_followups":@,@"lead_status":@
};
</code></pre>
<p>以下代码在 json 中隐藏:</p>
<pre><code> NSData *jsonData2 = ;
NSString *jsonString = [ initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
</code></pre>
<p>我已成功转换格式,但是...我无法传递多个字典。</p>
<p>谢谢</p>
<p>请帮帮我</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这里,</p>
<pre><code>"appointments" : [ {},{} ]
</code></pre>
<p>这是字典数组,因此您需要将 NSDictionary 添加到数组中,然后需要将该数组转换为 JSON 并将其传递给您的服务器。</p>
<pre><code> dicAppointment = [init];
arrAppointment = [init];
</code></pre>
<p>现在在循环的最后,您需要在 <code>arrAppointment</code></p> 中添加 <code>dicAppointment</code>
<p><strong>编辑</strong></p>
<p>这里你需要传递你的数组来代替字典</p>
<pre><code> NSDictionary *d = @{ @"appointments":arrAppointment ,@"lead_followups":arrStatus,@"lead_status":arrLeadStatus };
</code></pre>
<p>试试这个</p>
<p><strong>编辑2</strong></p>
<pre><code> arrAppointment = [init];
for (int i = 0; i < arrAppointmentData.count; i++)
{
dicAppointment = [init];
valueForKey:@"rowid"] forKey:@"local_id"];
valueForKey:@"id"] forKey:@"id"];
valueForKey:@"Fullname"] forKey:@"fullname"];
valueForKey:@"email"] forKey:@"email"];
valueForKey:@"mobileNo"] forKey:@"mobileno"];
valueForKey:@"date_time"] forKey:@"appt_datetime"];
valueForKey:@"address"] forKey:@"address"];
valueForKey:@"cityid"] forKey:@"city_id"];
valueForKey:@"stateid"] forKey:@"state_id"];
valueForKey:@"pincode"] forKey:@"pincode"];
valueForKey:@"speciality_id"] forKey:@"speciality_id"];
valueForKey:@"lead_comments"] forKey:@"lead_comments"];
valueForKey:@"appointment_comment"] forKey:@"appt_comments"];
valueForKey:@"enquiry_created_date"] forKey:@"enquiry_created_date"];
valueForKey:@"enquiry_created_by"] forKey:@"enquiry_created_by"];
valueForKey:@"intrested"] forKey:@"interested"];
valueForKey:@"lead_follow_by"] forKey:@"lead_followup_by"];
valueForKey:@"lead_follow_datetime"] forKey:@"lead_followup_datetime"];
valueForKey:@"lead_created_date"] forKey:@"lead_created_date"];
valueForKey:@"lead_created_by"] forKey:@"lead_created_by"];
valueForKey:@"appointment_created_date"] forKey:@"appt_created_date"];
valueForKey:@"appointment_created_by"] forKey:@"appt_created_by"];
valueForKey:@"initial_status"] forKey:@"initial_status"];
valueForKey:@"current_status"] forKey:@"current_status"];
valueForKey:@"hospital_id"] forKey:@"hospital_id"];
valueForKey:@"cashcredit"] forKey:@"cash_credit"];
valueForKey:@"expectdate"] forKey:@"expected_treatment_date"];
valueForKey:@"other_hospital"] forKey:@"other_hospital"];
valueForKey:@"create_plan_mail_sent"] forKey:@"create_plan_mail_sent"];
;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在ios中创建以下json字符串?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40653365/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40653365/
</a>
</p>
页:
[1]