Wrong json format for htmlBody swift -
when try create json dictionary wrong format semicolon instead of commas json
let jsondata = try? jsonserialization.data(withjsonobject: params)
params dictionary. tried using options: .prettyprinted same
result { "key": "value"; "key": "value" } instead of { "key": "value", "key": "value" }
i tried read file same result semicolon :(
update full code:
let params: dictionary<string, string> = ["country":"a" ,"language":"a" ,"query":"some query" ,"context": "null"] let baseurl = "someurl" let url = url(string: baseurl)! var request = urlrequest(url: url) request.httpmethod = "post" { request.httpbody = try jsonserialization.data(withjsonobject: params, options: .prettyprinted) print(try? jsonserialization.jsonobject(with: request.httpbody!)) } catch let error { print(error.localizeddescription) }
this output:
optional({ context = null; country = a; language = a; query = "some query"; })
the optional not problem semicolons
let json = try! jsonserialization.jsonobject(with: request.httpbody!, options: .allowfragments)
output:
{ context = null; country = a; language = a; query = "some query"; }
this line solved problem:
request.addvalue("application/json", forhttpheaderfield: "content-type")
Comments
Post a Comment