What does the SWIFT server require for JSON objects? Two

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.