1

I'm working on JSON serialization like below code:

  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:

{
  "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:

{
  "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.

Please help me, Thanks in Advance.

  • Possible duplicate of [How to convert a JSON string to a dictionary?](https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary) – Hexfire Dec 19 '17 at 13:06
  • Already I tried but I had get [String : String] format. But I want only below format. { "ActedTime" : "2017-09-19 12:04:12", "EventDate" : "2017-10-06 07:03:29" } – Nandu Psycho Dec 19 '17 at 13:07
  • This is fine. Simply convert your date string to actual date with `DateFormatter`, setting format as `yyyy-MM-dd hh:mm:ss`. – Hexfire Dec 19 '17 at 13:09
  • Sorry, my question is I stored the data in dictionary like below format: "{\n \"ActedTime\" : \"2017-09-19 12:04:12\",\n \"EventDate\" : \"2017-10-06 07:03:29\”}” but I don't want this type of format. I want only below format: { "ActedTime" : "2017-09-19 12:04:12", "EventDate" : "2017-10-06 07:03:29" } compare both responses. First one is seems to be like string, 2nd one is dictionary – Nandu Psycho Dec 19 '17 at 13:14
  • If I print the data of "parsedData", it seems to be dictionary format. I passing the same parsedData parameters to anther API, it seems to be String format. String format is not accepting my service, It accepts only dictionary format. – Nandu Psycho Dec 19 '17 at 13:16
  • Both variants are strings, there is no difference. Your statement: `but data is printed like dictionary` - it isn't. What you have printed is json-formatted string, which (of course) resembles dictionary. – Hexfire Dec 19 '17 at 13:18
  • But how can I pass the parameters like dictionary format... – Nandu Psycho Dec 19 '17 at 14:23

1 Answers1

0

Omit .prettyPrinted, the server doesn't care anyway.

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.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • it's working but stored data seems to be like string format. My requirement is only dictionary format. How? – Nandu Psycho Dec 19 '17 at 13:33
  • One of my API is not accepting string format, it accepting only JSON format. – Nandu Psycho Dec 19 '17 at 13:49
  • JSON **is** String – vadian Dec 19 '17 at 13:55
  • String is converted to dictionary let params = convertStringToDictionary(text: parsedDict!) pass the value like request.httpBody = try! JSONSerialization.data(withJSONObject: params, options: []); Failed to load: The data couldn’t be read because it isn’t in the correct format. – Nandu Psycho Dec 19 '17 at 14:08
  • The format depends on the values of the `content` and `accepts` headers. – vadian Dec 19 '17 at 14:15
  • how can get convert json string to dictionary format? – Nandu Psycho Dec 20 '17 at 07:31
  • and my API parameters accepting only {} this format. when I'm converting string to dictionary is seems like []. But this not accepting in my scenario. – Nandu Psycho Dec 20 '17 at 07:41
  • The data type to send via HTTP POST is **always** `String` (actually `String` as binary `Data`). The crucial point is what the string represents. – vadian Dec 20 '17 at 07:55