0

I have 3 arrays when I insert data inside table than that data also add in the array (key, value pair).

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

My problem is how to create JSON object and data store key value pair.

I want this

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

so that data passes to url()

In the action button that adds data in array and table

@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()

        }



    }

I want to generate JSON array from person, email and contact in key value pairs

4 Answers 4

3

to answer your question.

 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
Sign up to request clarification or add additional context in comments.

Comments

1

This is not a great design by choice to have multiple arrays relating to the data of same Entity.

Ideally create an Entity Model called Blog with fields like personName, email, mobileNo like below -

struct Blog {

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

And then in your code have an array of this to save the data then you can directly convert it into Json using the link

Convert Custom Structs to Json

Comments

0

Try this:

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
}

2 Comments

I pass your value in one paramerter like
var sss = ["cutomer name":"xyz","date":"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
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.