0

I want to send a list of the array to the server but I have troubles on it.

This is my backend information that I should send data like this.

This is my code:

loadRequest(request: "/tripTemporary/", parameters:
                ["startDate":"\(FindDriverViewController.dateToGo)",
                 "startTime":"\(FindDriverViewController.timeTOGo)",
                 "passengerNumber":"\(FindDriverViewController.PasengerCount)",
                 "typeId":"\(ServiceModeViewController.serviceMode)",
                 "originName":"\(MapViewController.sourceCity)",
                 "destinationName":"\(MapViewController.destinationCity)",
                 "passengerId":"\(ViewController.id)",
                    "tripCheckPoints":"{['address':'\(MapViewController.source)','longitude':'\(MapViewController.sourceLng)','latitude':'\(MapViewController.sourceLat)'],['address':'\(MapViewController.destination)','longitude':'\(MapViewController.Destinationlng)','latitude':'\(MapViewController.DestinationLat)']}"

                ],
                        json: true, get: false, callback: { (data) in

                            print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!)
            }, method: "POST")

Please help me.

2
  • Unrelated but replace NSString(data: data!, encoding: String.Encoding.utf8.rawValue)! with String(data: data!, encoding: .utf8)!. There is no reason to use NSString in this case. Commented Jul 1, 2018 at 6:59
  • thanks for your suggest. do you have any recommend for my problem? Commented Jul 1, 2018 at 7:19

1 Answer 1

2

A typical approach to be followed is to use the Encodable, you'll be able to get the desired JSON string without the need of the tedious manual handling.

First, you should define the struct for the request (and let it conforms to Encodable):

struct RequestFormat: Encodable {
    var startDate: String
    var startTime: String
    var passengerNumber: String
    var typeId: String
    var originName: String
    var destinationName: String
}

then you could directly use it after setting the desired info:

let encoder = JSONEncoder()
// dummy values
let myRequestObject = RequestFormat(startDate: "1/1/2018",
                                    startTime: "8:00PM",
                                    passengerNumber: "2",
                                    typeId: "10",
                                    originName: "Origin Name",
                                    destinationName: "Destination Name")

hence

do {
    let result = try encoder.encode([myRequestObject])
    if let resultString = String(data: result, encoding: .utf8) {
        print(resultString)
    }
} catch {
    print(error)
}

As you can see, the output of logging printing resultString would be:

[{"startTime":"8:00PM","typeId":"10","originName":"Origin Name","startDate":"1/1/2018","destinationName":"Destination Name","passengerNumber":"2"}]


Note that I encoded as an array (try encoder.encode([myRequestObject])), if you are aiming to get a single object, you could simply implement it as:

let result = try encoder.encode(myRequestObject)

Also, you could -obviously- add more that just one object to the encoded array:

let result = try encoder.encode([myRequestObject, myRequestObject2, myRequestObject3])

which myRequestObject, myRequestObject2 and myRequestObject3 are typed as RequestFormat.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks. i do your help and it works. but i don't know how should i POST to server. would you please help me?
In loadRequest, parameter is a string, correct? which means that you could pass resultString directly to it. What's the part that you are missing?\
no loadRequest, is [String:Any]. this is the part that i'm missing
@AmirhosseinKhodaei are you using any third party library? such as Alamofire?
in case only if we send as array: let result = try encoder.encode([myRequestObject]) , otherwise it should : let result = try encoder.encode(myRequestObject)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.