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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -