如何在swift3中以字典格式获取JSON响应?

问题描述 投票:1回答:1

我正在进行JSON序列化,如下面的代码:

  let jsonData: Data? = try? JSONSerialization.data(withJSONObject: abc, options: .prettyPrinted)
  let parsedDict = String(data: jsonData!, encoding: String.Encoding.utf8)
  print(" parse Dict Value \(parsedDict!)")

abc数据是:

{
  "ActedTime" = "2017-09-19 12:04:12",
  "EventDate" = "2017-10-06 07:03:29"
} 

序列化完成后,响应值为:

"{\n  \"ActedTime\" : \"2017-09-19 12:04:12\",\n  \"EventDate\" : \"2017-10-06 07:03:29\”}”

我打印了parsedDict,如下所示:

{
  "ActedTime" : "2017-09-19 12:04:12",
  "EventDate" : "2017-10-06 07:03:29"
} 

存储的数据看起来像字符串格式,但数据打印像字典。

如何获取字典格式以将参数发送到另一个API,如字典格式。

请帮助我,在此先感谢。

json nsdictionary json-serialization
1个回答
0
投票

忽略.prettyPrinted,服务器无论如何都不在乎。

let jsonData = try? JSONSerialization.data(withJSONObject: abc)

如果对象应该是字典,为什么要对它进行序列化呢?

但是,如果数据应该在POST请求中传递,因为httpBody传递jsonData而不是parsedDict

© www.soinside.com 2019 - 2024. All rights reserved.