MOMMH MOMMH - 6 months ago 42
iOS Question

Alamofire json back "=" not ":"

Hello I just implement Alamofire and it is working well but the result not json

let UrlString = urlString
let url = NSURL(string: UrlString)!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = hTTPMethod
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let HttpBodyString = httpBody
let data = HttpBodyString.dataUsingEncoding(NSUTF8StringEncoding)

request.HTTPBody = data

Alamofire.request(request)
// .validate()
.responseJSON { response in
if let JSON = response.result.value {

print("JSON: \(JSON)")
}


it gives me

JSON: (
{
"Area_ID" = 2;
"City_ID" = 5;
"House_Building_No" = 2;
"House_Floor" = 2;
"House_QRCode" = 1;
"Region_ID" = 6;
"Street_ID" = 2;
}
)


it's not json i want it with : not = also give me ; not ,

Answer

The response.result.value variable in Alamofire's responseJSON method is an NSDictionary. The printed output you see is the default way of printing NSDictionaries.

If you want to have a string containing JSON, expressed in the standard format (i.e. using ":" between keys and values), then try using the responseString method instead.

Alternatively, if you want to have a JSON object, you could use SwiftyJSON and do the following:

Alamofire.request(request).responseJSON {
    response in  
    if let jsonDictionary = response.result.value {
        let json = JSON(jsonDictionary)
        print(json)
    }
}
Comments