I have tried a lot of things now and are still getting: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'

UPDATE: I scan a barcode and save the info:

let calendar = Calendar.current
let expiryDate = (calendar as NSCalendar).date(byAdding: .month, value: 3, to: Date(), options: [])?.description

let barcode = BarcodeData(barcodeValue: value,
                          datetime: dateTime,
                          expiryDate: expiryDate!,
                          latitude: latitude.description,
                          longitude: longitude.description,
                          status: txtStatus.text!,
                          type: txtType.text!,
                          extraText: "")

Object are then mapped to a JSON string, it seems that the slashes (/) are added by this function:

let jsonBarcode = Mapper<BarcodeData>().toJSONString(barcode)

The barcode are then added to a list of String:

barcodeDataList.append(jsonBarcode)

When I click a button I invoke the web service, that anticipate parameters in the form of:

let testParams : Parameters =
    [ "udid": "my_udid",
       "data": jsonArray
    ]

jsonArray consist of an array of the BarcodeData-object(s) as seen above.

Communication with the web service looks like:

Alamofire.request(url, method: .post, parameters: testParams, encoding: JSONEncoding.default).validate().responseJSON { response in

                switch response.result {
                case .success:
                    print("Validation successful")

                    if let json = response.result.value {
                        print("JSON: \(json)")
                    }

                case .failure(let error):
                    print("Error: \(error)")
                }
            }

The following is passed to the ws:

    ["udid": "\"001-my_udid\"", "data": [
    "{\"latitude\":\"0.0\",\"status\":\"Empty\",\"datetime\":\"2016-09-20 05:10\",\"longitude\":\"0.0\",\"type\":\"ABC123\",\"barcodevalue\":\"123456\"}"
]]

The json array for "data" validates at jsonlint.com and the response from the server is in the form of a json object like:

{result: "Data successfully received"}
share|improve this question
    
what are those values Parameters and jsonArray? – Ganesh Kumar Sep 20 '16 at 4:20
    
Try to replace \" in your response JSON with " in IOS end and rest of the things will be fine. – Abdul Rehman Warraich Sep 20 '16 at 4:24
    
Abdul: As you can see on my edited post it seems that ObjectMappers function are adding the slashes (/). – Ramon Sep 20 '16 at 17:16
    
As far as I know it is okay to have slashes in a JSON object according to the specification. So it shouldn't be the issue here, unless if Alamofire does not support it. Which would be weird since Alamofire are often used with ObjectMapper which actually adds the slash symbols. – Ramon Sep 22 '16 at 2:11

I think you need to encode your array in json object try something like this

    do{
        let data = try NSJSONSerialization.dataWithJSONObject(mappingArray, options: NSJSONWritingOptions(rawValue: 0))
        let mappedString : String = (String(data: data , encoding: NSUTF8StringEncoding)!)
        return mappedString
    }
    catch{
        print(error)
        return error as! String
    }

Append this string with your url

share|improve this answer
    
I wrapped the code snippet in a function but the result was only that even more slashes are being added :-( – Ramon Sep 20 '16 at 17:17

Change your encoding in request from encoding: JSONEncoding.default to encoding: URLEncoding.default

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.