如何在ios swift中创建json对象

时间:2017-07-17 07:39:02

标签: ios arrays json swift

当我在表中插入数据时,我有3个数组,而这些数据也会添加到数组中(键,值对)。

var person = ["ABC","XYZ","PQR"]
var email = ["abc@yahoo.com","xyz@yahoo.com","pqr@yahoo.com"]
var mobile = ["1234567890","1234567890","1234567890"]

我的问题是如何创建JSON对象和数据存储键值对。

我想要这个

{
    "blogs": [
        {
            "person": "ABC",
            "email": "abc@yahoo.com",
            "contact": "1234567890"
        },
        {
            "person": "XYZ",
            "email": "xyz@yahoo.com",
            "contact": "1234567890"
        },
{
            "person": "PQR",
            "email": "pqr@yahoo.com",
            "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数组

4 个答案:

答案 0 :(得分:1)

选择具有与同一实体的数据相关的多个数组,这不是一个很好的设计。

理想情况下,创建一个名为Blog的实体模型,其中包含personName,email,mobileNo等字段,如下所示 -

struct Blog {

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

然后在你的代码中有一个这样的数组来保存数据然后你可以使用链接将它直接转换为Json

Convert Custom Structs to Json

答案 1 :(得分:0)

回答你的问题。

 var person = ["ABC","XYZ","PQR"]
    var email = ["abc@yahoo.com","xyz@yahoo.com","pqr@yahoo.com"]
    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

答案 2 :(得分: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
}

答案 3 :(得分:0)

let dictionary = ["key1": "value1", "key2": "value2"] 
let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted) 
// Verifying it worked: 
let parsedObject = try! JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments)