2017-04-22 2 views
0

JSONデータを文字列に変換する際に問題が発生しています。 Alamofireは別のURLにURLをオン何らかの理由Alamofire 4.4.0がエスケープされたJSON文字列(Swift 3)でJSON文字列を変換するのはなぜですか?

var url = "http://10.1.10.98/POSSytem/api/inventories/PutInventory?received={'id':'coke','price':4.99,'In_stock':2}" 
print(url) 
Alamofire.request(str, method: .post, parameters: [:], encoding: JSONEncoding, headers: nil) 
     .response { (res) in 
      print(res) 
} 

LOGS: original string ->http://10.1.10.98/POSSytem/api/inventories/PutInventory?received= {'id':'coke','price':4.99,'In_stock':2}

DefaultDataResponse(request: nil, response: nil, data: Optional(0 bytes), error: Optional(Alamofire.AFError.invalidURL(" http://10.1.10.98/POSSytem/api/inventories/PutInventory?received= {\'id\':\'coke\',\'price\':4.99,\'In_stock\':2}")), timeline: Timeline: { "Request Start Time": 514563518.605, "Initial Response Time": 514563518.597, "Request Completed Time": 514563518.597, "Serialization Completed Time": 514563518.605, "Latency": -0.007 secs, "Request Duration": -0.007 secs, "Serialization Duration": 0.007 secs, "Total Duration": 0.000 secs }, _metrics: nil)

。エスケープされた文字をJSON文字列で追加します。 alamofire 4.4.0 それは宿泊している必要があります。 "http://10.1.10.98/POSSytem/api/inventories/PutInventory?received= { 'ID': 'コークス'、 '価格':4.99、 'IN_STOCK':2}"

答えて

0
guard let js = try? JSONSerialization.data(withJSONObject: dict, options: []) else { 
      print("Error parsing json data \(dict)") 
      return 
     } 

    let theJSONText = String(data: js,encoding: .ascii) 


    url += "\(theJSONText!)" 



    let finalURL = url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) 


    Alamofire.request(finalURL!, method: .post) 
     .response { (res) in 
      print(res) 
    } 

addingPercentEncoding

を使用した後JSON文字列を有効なURLにエンコードし、ポストコールがトリガーされました。

関連する問題