快速发送带有对象数组的JSON作为Alamofire中的参数

时间:2019-07-17 00:48:10

标签: ios json swift alamofire lumen

我对Swift和Alamofire还是很陌生,我想要完成的就是发送一个数据结构,如下所示:

{
    "name" : "Test name",
    "intention" : "Purpose of practice test",
    "id_frequency" : "1",
    "member": [
        {
        "id_member" : "1",
        "email" : "member1@gmail.com",
        "id_member_type" : 1
        },
        {
        "id_member" : "4",
        "email" : "member2@gmail.com",
        "id_member_type" : 3
        },
        {
        "id_member" : "7",
        "email" : "member3@gmail.com",
        "id_member_type" : 3
        },
        {
        "id_member" : "5",
        "email" : "member4@gmail.com",
        "id_member_type" : 3
        },
        {
        "id_member" : "6",
        "email" : "member5@gmail.com",
        "id_member_type" : 3
        }
    ]
}

我着手构建所需json的方式如下:

 var membersArray = [AnyObject]()
 for i in 0..<members.count {
     let json: [String: Any]  = [
         "id_member": members[i].idMember!,
         "email": members[i].email!,
         "id_member_type": "\(Int(members[i].idMemberType)!)",
     ]
     membersArray.append(json as AnyObject)
 }

 let jsonMembers = JSON(membersArray)

 let jsonObject: [String: Any]  = [
     "member" : jsonMembers,
     "name": name!,
     "intention": intention!,
     "id_frequency": frequency!
 ]

 let jsonUpdate = JSON(jsonObject)

有了这个结构化的json,上面的行(jsonUpdate)。我继续执行webService。

 WevServices.createRequest(requestInfo: jsonUpdate) { (result) in
     print(result)
 }

我的网络服务方法如下:

static func createRequest(requestInfo: JSON, completion: @escaping (_ result: String) -> Void){

    let url = URL(string: "http://ws.someURL.com/CreateRequest")

    let parameters: [String : Any] = ["name" : "\(requestInfo["name"])", "intention" : "\(requestInfo["intention"])", "id_frequency" : "\(requestInfo["id_frequency"])", "member": requestInfo["member"]]

    Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
        print(response)

    }
}

我从服务器收到一条错误消息,提示发送数据不正确。

注意:我的应用程序使用Lumen作为后端。

1 个答案:

答案 0 :(得分:0)

做您所需的最好方法是:

func JSONToString(json: [String : String]) -> String?{
    do {
        let mdata =  try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // json to the data
        let convertedString = String(data: mdata, encoding: String.Encoding.utf8) // the data will be converted to the string
        print("the converted json to string is : \(convertedString!)") // <-- here is ur string

        return convertedString!

    } catch let myJSONError {
        print(myJSONError)
    }
    return ""
}

并针对alamofire请求执行此操作:

static func createRequest(requestInfo: [String :String], completion: @escaping (_ result: String) -> Void){

    let url = URL(string: "http://ws.someURL.com/CreateRequest")

    let parameters: [String : Any] = ["request" : JSONToString(json : requestInfo)!]

    Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
        print(response)

    }