将C#JSON POST转换为SWIFT 3
我一直在尝试将这段代码从C#转换为Swift 3.0,但在语法方面没有什么进展。该代码直接来自,但没有快速的示例 谢谢你的帮助和指点将C#JSON POST转换为SWIFT 3,c#,ios,swift,C#,Ios,Swift,我一直在尝试将这段代码从C#转换为Swift 3.0,但在语法方面没有什么进展。该代码直接来自,但没有快速的示例 谢谢你的帮助和指点 string text = "3GNDA13D76S000000;5XYKT3A12CG000000;"; string url = @"https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/"; var nameValues = new Dictionary<string, string
string text = "3GNDA13D76S000000;5XYKT3A12CG000000;";
string url = @"https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/";
var nameValues = new Dictionary<string, string>();
nameValues.Add("data", text);
nameValues.Add("format", "json");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
// using FormUrlEncodedContent
var name = new FormUrlEncodedContent(nameValues);
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
System.Threading.CancellationToken token = new
System.Threading.CancellationToken();
try {
var tmp = client.PostAsync(client.BaseAddress, name, token).Result;
var result = tmp.Content.ReadAsStringAsync();
} catch (Exception err) {
// error handling
}
string text=“3GNDA13D76S000000;5XYKT3A12CG000000;”;
字符串url=@“https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/";
var nameValues=新字典();
名称值。添加(“数据”,文本);
添加(“格式”、“json”);
HttpClient=新的HttpClient();
client.BaseAddress=新Uri(url);
//使用FormUrlEncodedContent
变量名称=新的FormUrlEncodedContent(名称值);
client.DefaultRequestHeaders.Accept.Add(新建)
MediaTypeWithQualityHeaderValue(“应用程序/json”);
System.Threading.CancellationToken令牌=新建
System.Threading.CancellationToken();
试一试{
var tmp=client.PostAsync(client.BaseAddress,name,token).Result;
var result=tmp.Content.ReadAsStringAsync();
}捕获(异常错误){
//错误处理
}
我用测试VIN对url进行了硬编码,这对我很有效,谢谢你的帮助
let url: String = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVIN/WBY2Z2C52GV676091?format=JSON"
Alamofire.request(url, parameters: nil, encoding: JSONEncoding.default).responseJSON {
(response) in
print(response.request as Any) // original URL request
print(response.response?.allHeaderFields as Any) // URL response
print(response.result.value as Any) // result of response serialization
}
看一看在Swift中使用Alamofire进行网络请求。看一看谢谢伙计们,我会检查Alamofire的这一点以及提供的链接。我对Swift还是新手,JSON让我很困惑,所以非常感谢。