C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c# restful使用

C# RESTful完整使用实战示例

作者:wangnaisheng

RESTful是一种轻量级、跨平台的Web服务架构风格,C# 中使用 RESTful API主要涉及客户端调用和服务器端实现两个方面,接下来通过本文介绍C# RESTful完整使用实战示例,感兴趣的朋友一起看看吧

一、RESTful基础

RESTful是一种轻量级、跨平台的Web服务架构风格,它的核心原则是:

HTTP方法操作幂等安全
GET查询
POST新增
PUT更新
DELETE删除

RESTful vs 传统API

二、调用RESTful API的通用流程

// 1. 获取数据 - HttpClient最佳实践
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://api.example.com/data");
var jsonString = await response.Content.ReadAsStringAsync();
// 2. 解析JSON - 两种常用方式
// 方式一:动态解析(快速提取少量字段)
var jsonDoc = JsonDocument.Parse(jsonString);
string name = jsonDoc.RootElement.GetProperty("user").GetProperty("name").GetString();
// 方式二:强类型解析(推荐!)
public class User {
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime RegisterDate { get; set; }
}
var user = JsonSerializer.Deserialize<User>(jsonString, new JsonSerializerOptions {
    PropertyNameCaseInsensitive = true // 忽略大小写
});
// 3. 使用解析后的数据
Console.WriteLine($"欢迎,{user.Name}!您已注册于{user.RegisterDate:yyyy-MM-dd}");

三、JSON解析的实用技巧

3.1 推荐方案

使用System.Text.Json(.NET Core 3.0+)

优势

3.2 常见坑点及解决方案

问题解决方案代码示例
字段大小写不一致PropertyNameCaseInsensitive = truenew JsonSerializerOptions { PropertyNameCaseInsensitive = true }
日期格式问题添加日期转换器options.Converters.Add(new DateTimeConverter("yyyy-MM-dd"));
JSON中有注释忽略注释options.ReadCommentHandling = JsonCommentHandling.Skip;
空字段导致异常设置默认值public string Name { get; set; } = string.Empty;

四、完整实战示例

using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
public class WeatherApiService
{
    public async Task<WeatherData> GetWeatherAsync(string location)
    {
        using var httpClient = new HttpClient();
        // 获取天气数据
        var response = await httpClient.GetAsync(
            $"https://api.weather.com/v3?location={location}");
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();
        // 强类型解析(推荐方式)
        return JsonSerializer.Deserialize<WeatherData>(json, new JsonSerializerOptions {
            PropertyNameCaseInsensitive = true,
            NumberHandling = JsonNumberHandling.AllowReadingFromString
        });
    }
}
public class WeatherData
{
    public string City { get; set; }
    public string Condition { get; set; }
    public int Temperature { get; set; }
    public DateTime ForecastDate { get; set; }
}

五、为什么推荐System.Text.Json?

  1. 性能更好:比Newtonsoft.Json快约20-30%
  2. 原生集成:.NET Core 3.0+默认包含
  3. 安全:不依赖第三方库,减少安全风险
  4. 现代化:支持最新的JSON特性

六、进阶建议

  1. 创建通用API服务类:把HttpClient封装起来,避免重复代码
  2. 处理错误:添加try-catch块处理网络异常
  3. 缓存:对频繁请求的API添加缓存机制
  4. 异步:始终使用异步方法,提升应用性能

七、处理JSON嵌套复杂结构

三步搞定嵌套JSON处理(推荐System.Text.Json)

7.1 第一步:定义强类型模型(关键!)

// 定义嵌套结构
public class ApiResponse
{
    public string Status { get; set; }
    public Data Data { get; set; }
}
public class Data
{
    public List<User> Users { get; set; }
    public Meta Meta { get; set; }
}
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
}
public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}
public class Meta
{
    public int TotalCount { get; set; }
    public int Page { get; set; }
    public int PageSize { get; set; }
}

7.2 第二步:解析嵌套JSON(一行代码搞定!)

// 获取API返回的JSON字符串
string json = await httpClient.GetStringAsync("https://api.example.com/data");
// 一行代码解析嵌套结构(这才是真正的"快速"!)
var response = JsonSerializer.Deserialize<ApiResponse>(json, new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true, // 忽略大小写
    NumberHandling = JsonNumberHandling.AllowReadingFromString // 处理数字字符串
});

7.3 第三步:安全访问嵌套数据

// 安全访问嵌套数据
if (response != null && response.Data != null && response.Data.Users != null)
{
    foreach (var user in response.Data.Users)
    {
        Console.WriteLine($"用户: {user.Name}, 城市: {user.Address.City}");
    }
    // 也可以直接访问
    Console.WriteLine($"总用户数: {response.Data.Meta.TotalCount}");
}

八、实用技巧:处理常见嵌套问题

8.1 嵌套层级太深,字段可能为空

// 安全访问嵌套字段(避免NullReferenceException)
var city = response?.Data?.Users?[0]?.Address?.City ?? "N/A";
Console.WriteLine($"城市: {city}");

8.2 JSON字段名大小写不一致

new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true // 关键!忽略大小写
}

8.3 嵌套结构中可能有额外字段

// 忽略未知字段
new JsonSerializerOptions
{
    IgnoreUnknown = true
}

8.4 处理嵌套数组

// 获取所有用户的姓名
var userNames = response.Data.Users
    .Where(u => u.Id > 100)
    .Select(u => u.Name)
    .ToList();

一个真实案例:处理天气API的嵌套JSON

public class WeatherResponse
{
    public CurrentWeather Current { get; set; }
    public Forecast Forecast { get; set; }
}
public class CurrentWeather
{
    public string City { get; set; }
    public int Temperature { get; set; }
    public string Condition { get; set; }
}
public class Forecast
{
    public List<DailyForecast> Days { get; set; }
}
public class DailyForecast
{
    public DateTime Date { get; set; }
    public int HighTemp { get; set; }
    public int LowTemp { get; set; }
}
// 使用方式
var weather = JsonSerializer.Deserialize<WeatherResponse>(json);
Console.WriteLine($"当前城市: {weather.Current.City}, 温度: {weather.Current.Temperature}°C");
Console.WriteLine($"明天最高温: {weather.Forecast.Days[0].HighTemp}°C");

到此这篇关于C# RESTful完整使用实战示例的文章就介绍到这了,更多相关c# restful使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文