2017-07-17 316 views
-1

我有3個數組,當我在表中插入數據時,數據也添加到數組中(鍵值對)。如何在ios swift中創建json對象

var person = ["ABC","XYZ","PQR"] 
var email = ["[email protected]","[email protected]","[email protected]"] 
var mobile = ["1234567890","1234567890","1234567890"] 

我的問題是如何創建JSON對象和數據存儲鍵值對。

我想要這個

{ 
    "blogs": [ 
     { 
      "person": "ABC", 
      "email": "[email protected]", 
      "contact": "1234567890" 
     }, 
     { 
      "person": "XYZ", 
      "email": "[email protected]", 
      "contact": "1234567890" 
     }, 
{ 
      "person": "PQR", 
      "email": "[email protected]", 
      "contact": "1234567890" 
     } 
    ] 
} 

,使得數據傳送到url()

在操作按鈕,在陣列和臺增加數據

@IBAction func meeting_info(_ sender: Any) { 

     var PersonName = person_name.text 
     var Email = email_id.text 
     var MobileNo = mobile_no.text 

     if (person_name.text?.isEmpty)! || (email_id.text?.isEmpty)! || (mobile_no.text?.isEmpty)! { 

      displayMyAlertMessage(userMessage: "please check field empty or not"); 

     } 

     else{ 

      person.append(person_name.text!) 
      email.append(email_id.text!) 
      mobile.append(mobile_no.text!) 

      meetingTableView.reloadData() 

     } 



    } 

我要生成從人JSON數組,電子郵件和關鍵值對中的聯繫人

回答

1

這不是通過選擇具有與同一實體的數據相關的多個陣列的偉大設計。

理想的情況下創建一個像PERSONNAME,電子郵件,mobileNo像下面領域的實體模型稱爲博客 -

struct Blog { 

var personName: String? 
var email: String? 
var mobileNo: String? 
} 

然後在你的代碼有這樣的數組來保存數據,那麼你可以直接把它轉換成使用JSON鏈接

Convert Custom Structs to Json

0

回答你的問題。

var person = ["ABC","XYZ","PQR"] 
    var email = ["[email protected]","[email protected]","[email protected]"] 
    var mobile = ["1234567890","1234567890","1234567890"] 


    var paramCollection = [Any]() 

    var index = 0 
    for personData in person { 
     var dataCollection = [String:Any]() 
     dataCollection["person"] = personData 
     dataCollection["email"] = email[index] 
     dataCollection["contact"] = mobile[index] 
     paramCollection.append(dataCollection) 
     index += 1 
    } 

    let finalParameter = ["blogs":paramCollection] 
} 

//This will do the trick but to make it more robust you should rethink your design 
// maybe use struct to store a persons data 
struct Blog { 
    var person: String 
    var email: String 
    var mobile: String 

    init(name:String, email:String, phone:String) { 
     self.person = name 
     self.email = email 
     self.mobile = phone 
    } 
} 

//and instead of having three arrays holding three different property, you can have one array of 
var blogArray = [Blog]() 

//You understand where I'm going with this 
0

試試這個:

let jsonObject: [String: Any]? 
let array: [[String: Any]] = [[:]] 

    for i in 0..person.count { 
    let dict = ["person": person[i], 
     "email": email[i], 
     "contact": mobile[i]] 
    array.append(dict) 
    } 
    jsonObject = ["blogs": array] 

let validateJson = JSONSerialization.isValidJSONObject(jsonObject) 
if validateJson { 
    //Go Ahead 
} 
+0

我在一個paramerter傳遞你的價值就像 –

+0

VAR SSS = [ 「CUTOMER名」: 「XYZ」, 「日期」: 「13-08-2017」 「blogs」:array] as [String:Any] and coverter into jsonobject and that jsonobject pass to post web service.any probelm for future process –