将字典的字典转换成jsonObject以作为RequestBody swift发送

时间:2019-06-19 21:47:26

标签: ios json swift dictionary

我正在尝试将其中具有字典数组的Dictionary转换为Json Object,作为其中的JsonObject的JsonArray来发送请求,但我总是收到错误消息:

“ ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'JSON写入(NSURL)中的类型无效''

我尝试通过以下方式进行转换:

 requestBody = ["audio": finalMap]
 let jsonData = try? JSONSerialization.data(withJSONObject: 
   requestBody)

-----This is the Dictionary -------
      [
   "audio": [
     [
      "__type":, "filename": , 
      "content":data1.wav,
      "speech": {
                 "__type" =;
                  flags = 3;
                  grammar = "in";
                 languageCode = "en-US";
                 name = pin;
               }, 
      "name": ], 
       [
      "__type":, "filename": , 
      "content":data2.wav,
      "speech": {
                 "__type" =;
                  flags = 3;
                  grammar = "in";
                 languageCode = "en-US";
                 name = pin;
               }, 
      "name": ],
      [
      "__type":, "filename": , 
      "content":data3.wav,
      "speech": {
                 "__type" =;
                  flags = 3;
                  grammar = "in";
                 languageCode = "en-US";
                 name = pin;
               }, 
      "name": ],,
    ]
 ]

-----这是预期的结果-------

{
  "audio": [
{
  "__type": "SpeechVerifierAudio",
  "name": "string",
  "filename": "string",
  "content": "string",
  "speech": [
    {
      "__type": "SpeechContext",
      "name": "string",
      "grammar": "string",
      "languageCode": "string",
      "phrases": [
        "string"
      ],
      "flags": 0
    }
  ]
}
 ]
}

1 个答案:

答案 0 :(得分:1)

似乎您的问题是字典中有NSUrl,您不能将其转换为URL吗?

(URL是可编码的,而NSUrl是不可编码的)