简体   繁体   English

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

[英]How can I get the JSON response in dictionary format in swift3?

I'm working on JSON serialization like below code: 我正在像以下代码一样进行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 data is: abc数据为:

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

After completion of serialization, the response value is: 序列化完成后,响应值为:

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

I printed the parsedDict like below: 我打印了parsedDict,如下所示:

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

The stored data seems like string format, but data is printed like dictionary. 存储的数据看起来像字符串格式,但是数据却像字典一样打印。

How can I get dictionary format for sent the parameters to another API like dictionary format. 如何获取字典格式,以将参数发送到另一个API,例如字典格式。

Please help me, Thanks in Advance. 请帮助我,在此先感谢。

Omit .prettyPrinted , the server doesn't care anyway. 省略.prettyPrinted ,服务器无论如何都不在乎。

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

And if the object is supposed to be a dictionary, why do you serialize it at all? 而且如果该对象应该是字典,那么为什么还要序列化它呢?

However if the data is supposed to be passed in a POST request as httpBody pass jsonData rather than parsedDict . 但是,如果数据应该作为httpBody传递jsonData而不是parsedDict在POST请求中传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM