使用C#和iOS从API获取JSON地址的过程如下:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
string apiUrl = "https://api.example.com/data.json";
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
// 在这里处理JSON数据
}
else
{
Console.WriteLine("请求失败: " + response.StatusCode);
}
}
}
}
import Foundation
func fetchJSONData() {
let apiUrl = URL(string: "https://api.example.com/data.json")!
let task = URLSession.shared.dataTask(with: apiUrl) { (data, response, error) in
if let error = error {
print("请求失败: \(error)")
return
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
// 在这里处理JSON数据
} catch {
print("JSON解析失败: \(error)")
}
}
}
task.resume()
}
你可以通过以下链接了解更多关于腾讯云相关产品的信息:
领取专属 10元无门槛券
手把手带您无忧上云