Remember the parameter format of the Last Post request?
"{' name ':d og ', ' password ': ' 123 '}"
Why do you write this?
One: Network programming, you need a sharp tool to grab the package tools, Fiddler This is good, I found caught in the HTTP contentType ="application/x-www-form-urlencoded "Well, what I want is contenttype=" Application/json ", go to Yyhrequest to see where the settings are,
Func serializerequestparameters () { ContentType = "application/x-www-form-urlencoded" if (method = = "GET") { if let Queryurl = Queryparametersurl () { URL = queryurl } } else { BODY = serializedrequestbody ()
} }
Ah, here it is, immediately changed tocontenttype= "Application/json", all right, run and see, still no egg .
Two: Then follow the original route.
Just write a way, simply.
Func Converttype (parameters:dictionary<string,string>)->string{ var para:dictionary<string, string> = Parameters var parastr:string= "{" for (key, value) in para{ parastr = parastr + "'" + Key + "'" + ":" + "'" + Value + "'" + "," } let Rangeofpara = Range (Start:paraStr.startIndex, end:advance (Parastr.endin Dex,-1)) let finalstr = Parastr.substringwithrange (Rangeofpara) var parametersstr:string = finalstr + "}" Return PARAMETERSSTR }
In that case, you can turn the dictionary into the desired "{' name ':d og ', ' password ': ' 123 '}" type
If there are multiple, multi-layered, then you need to constantly loop to add
var strtest:[string]=[] var n:int=5 for i in 0...n{let parameter1:dictionary<string,string> = [" A ":" 1 "] let parameter2:dictionary<string,string> = [" A ":" 1 "] Strtest.insert (" {"+self. Converttype (parameter1) + self. Converttype (parameter2) + "}", AtIndex:strTest.endIndex) }println (strtest)
So the final production of the server needs the format of roughly the following
"[{' a ': {' AA ': ' 1 '}, ' B ': [{' A ': ' 1 '},{}]},{},{},{},{}]"
Three: summary:
In fact, the main idea is: First look at what format you want, for example, I want is "{' name ':d og ', ' password ': ' 123 '}" this type, and then use the method of the for key value in the variable dictionary to the value, to him into the final format you want
What does the SWIFT server require for JSON objects? Two