2017-07-17 11 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オブジェクトとデータストアのキー値のペアを作成する方法です。

私は人から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() 

     } 



    } 

たい、電子メールと連絡先のキーバリューのペア

答えて

1

これはありません同じエンティティのデータに関連した複数の配列を持つことができます。

struct Blog { 

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

そして、あなたのコードでは、あなたが直接に変換することができ、データを保存するには、この配列を持っている -

理想的PERSONNAME、電子メール、以下のようなmobileNoのようなフィールドを持つエンティティモデルと呼ばれるブログを作成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

私は –

+0

のvar SSSのように1 paramerterであなたの値を渡す= [ "cutomer名": "XYZ"、 "日付": "13-08-2017"、 "blogs":array]を[String:Any]とし、jsonobjectにカバレッジを入れ、jsonobjectがWebサービスを投稿するために渡します。 –

関連する問題