1

i'm porting my really simple app from javascript (embedded in an app) to ios8/swift and Xcode. The app will insert some simple event in google calendar using CalendarApiV3.

In javascript it was easy to create a simple data structure to pass in a http POST request, like this structure :

var data:struct =
    {
        "end": {
            "dateTime": "20141223T12:25:00Z"
        },
        "start": {
            "dateTime": "20141223T10:25:00Z"
        },
        "summary": "description of event",
        "reminders": {
            "useDefault": false,
            "overrides": [
            {
            "method": "sms",
            "minutes": "60"
            },
            {
            "method": "email",
            "minutes": "60"
            }
            ]
        }
    };

Ok, how to re-create the some structure in Swift ? I ended up looking for swifty json, but they all tell me how to PARSE json requested, not how to FORM a json request. I hope i was clear.

Thanks in advance.

Victor

2 Answers2

4

Here's a very simple example and I assume that nobody wants to deal with JSON strings, they want to have them created from data structures.

var dict1 = ["dave": "drangle", "hume": "cronyn"]
var dict2 = ["bob": "fossil", "vince": "powers"]
var ary = [dict1, dict2]
var jsonData = JSON(ary)
var post:NSData = jsonData.rawData()!;
var postLength:NSString = String(post.length)

var url:NSURL = NSURL(string: "https://some.server.com/mobile.php")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = post
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {
   println(data)
}

As you can see, it's an array of dictionaries which then gets converted to a SwiftyJSON struct. The jsonData.rawData()! is what gets you NSData which is the required datatype for request.HTTPBody. Here's what you see on the PHP server side of things using file_get_contents("php://input") and print_r(json_decode()):

[{"hume":"cronyn","dave":"drangle"},{"bob":"fossil","vince":"powers"}]
Array
(
   [0] => stdClass Object
   (
       [hume] => cronyn
       [dave] => dangler
   )

   [1] => stdClass Object
   (
       [bob] => fossil
       [vince] => powers
   )

)
T. Wells
  • 41
  • 3
1

SwiftyJSON is great for building JSON object from AnyObject in swift.

You can take a look at the site about how to create JSON object from AnyObject.

After you have your own JSON object, the site actually mentioned about how to creating JSON string you can use in HTTP Request.

if let string = json.rawString() {
    //Do HTTP Request here
}

here cezar answered greatly about how to use HTTP Request in swift : How to make an HTTP request in Swift?

Update:

So I assume that you have jsonString available, you can use this to make your swiftyJSON object

let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let jsonObject = JSON(data: data!)
Community
  • 1
  • 1
ramacode
  • 914
  • 6
  • 14