3

我想制作一本字典并将这些数据与使用 alamofire 上传的视频文件一起传递。下面是我的json。

{
    "taskid": "2",
    "task_type": "N\/A",
    "fileName": null,
    “videofile”:
    "task": [
        {
            “Cid": 1,
            “Start”: "00:00:00",
            “End”: "00:00:00"
        },

        {
            “Cid": 2,
            “Start”: "00:00:00",
            “End”: "00:00:00"
        },
        {
           “Cid": 3,
            “Start”: "00:00:00",
            “End”: "00:00:00"
        },
    ]
}

以下字典数组的代码应该是什么?如何通过?

multipartFormData.append((self.singleTonFile?.fileUrl)! , withName: "videofile" , fileName: fileNameOnly + ".webm", mimeType: "video/webm")
multipartFormData.append(self.singleTon.userid.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "userid")

multipartFormData.append(self.singleTon.taskid.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "taskid")

multipartFormData.append(self.singleTon. filename.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "filename")

@riddhi。- chk 我的一个样本的解决方案。这是一种不同的格式。但遵循字典数组作为参数..

for i in 0..<self.singleTon.individualTask.count
        {
            let dictTask = ["id":self.singleTon.individualTask[i],
                        "start_time":self.singleTon.individualTaskStartTime[i],
                        "end_time":self.singleTon.individualTaskEndTime[i],
                        ]
            parameters.append(dictTask as [String : AnyObject])
        }
           ]
        var dataParameters = Data()
        do
        {
            dataParameters = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions(rawValue: 0))
            let objectString = String(data: dataParameters, encoding: .utf8)

            print("objectString : ", objectString!)
        }
        catch
        {

        }

        var alamoDict = [String: Any]()
        if let scrn: NSScreen = NSScreen.main() {
            let rect: NSRect = scrn.frame
            let height = rect.size.height
            let width = rect.size.width
            self.singleTon.display = "\(height)" + "X" + "\(width)"
        }
         print("self.singleTon.processor  ", self.singleTon.processor)
            print("resolution  ", self.singleTon.display)
        alamoDict = ["task":parameters,
                         "userid":self.singleTon.userId,
                         "video_duration":self.singleTon.video_duration,
                         "fileName":self.singleTon.fileNameForUpload,
                         "User_type":self.singleTon.User_type,
                         "OS":self.singleTon.OS,
                         "processor":self.singleTon.processor,
                         "task_type":self.singleTon.task_type,
                         "taskid":self.singleTon.taskIdPassed,
                         "memory":self.singleTon.memory,
                         "Freememory":self.singleTon.freememory,
                         "display":self.singleTon.display,
                         ]

        var dataAlamoDict = Data()

// 这是为了测试 json 序列化

    *

do
        {
            dataAlamoDict = try JSONSerialization.data(withJSONObject: alamoDict, options: JSONSerialization.WritingOptions(rawValue: 0))//NSJSONSerialization.dataWithJSONObject(array, options: nil, error: nil)
            let objectString = String(data: dataParameters, encoding: .utf8)

            print("objectString : ", objectString!)
        }

   //This is ur alamofire format


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(dataAlamoDict, withName: "TaskInfo")
        //  …… etc ec*

*

4

1 回答 1

3

帮助像riddhi这样的人

@riddhi。- 检查我的样本之一的解决方案。这是一种不同的格式。但遵循字典数组作为参数..

for i in 0..<self.singleTon.individualTask.count
        {
            let dictTask = ["id":self.singleTon.individualTask[i],
                        "start_time":self.singleTon.individualTaskStartTime[i],
                        "end_time":self.singleTon.individualTaskEndTime[i],
                        ]
            parameters.append(dictTask as [String : AnyObject])
        }
           ]
        var dataParameters = Data()
        do
        {
            dataParameters = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions(rawValue: 0))
            let objectString = String(data: dataParameters, encoding: .utf8)

            print("objectString : ", objectString!)
        }
        catch
        {

        }

        var alamoDict = [String: Any]()
        if let scrn: NSScreen = NSScreen.main() {
            let rect: NSRect = scrn.frame
            let height = rect.size.height
            let width = rect.size.width
            self.singleTon.display = "\(height)" + "X" + "\(width)"
        }
         print("self.singleTon.processor  ", self.singleTon.processor)
            print("resolution  ", self.singleTon.display)
        alamoDict = ["task":parameters,
                         "userid":self.singleTon.userId,
                         "video_duration":self.singleTon.video_duration,
                         "fileName":self.singleTon.fileNameForUpload,
                         "User_type":self.singleTon.User_type,
                         "OS":self.singleTon.OS,
                         "processor":self.singleTon.processor,
                         "task_type":self.singleTon.task_type,
                         "taskid":self.singleTon.taskIdPassed,
                         "memory":self.singleTon.memory,
                         "Freememory":self.singleTon.freememory,
                         "display":self.singleTon.display,
                         ]

        var dataAlamoDict = Data()

// 这是为了测试json序列化格式

    *

do
        {
            dataAlamoDict = try JSONSerialization.data(withJSONObject: alamoDict, options: JSONSerialization.WritingOptions(rawValue: 0))//NSJSONSerialization.dataWithJSONObject(array, options: nil, error: nil)
            let objectString = String(data: dataParameters, encoding: .utf8)

            print("objectString : ", objectString!)
        }

   //This is ur alamofire format


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(dataAlamoDict, withName: "TaskInfo")
        //  …… etc ec* type ur rest of the code

*

于 2018-05-02T06:30:10.983 回答