using System.Text.Json.Serialization;
namespace Dpz.Core.Web.Models;
public class NewsResponse
{
public int Code { get; set; }
public TodayNewsModel? Data { get; set; }
public string? Message { get; set; }
}
public class TodayNewsModel
{
public Calendar? Calendar { get; set; }
public List<History>? HistoryList { get; set; }
public List<News>? NewsList { get; set; }
public Phrase? Phrase { get; set; }
public Sentence? Sentence { get; set; }
public Poem? Poem { get; set; }
public Weather? Weather { get; set; }
}
/// <summary>
/// 日期
/// </summary>
public class Calendar
{
/// <summary>
/// 年份
/// </summary>
[JsonPropertyName("cYear")]
public int Year { get; set; }
/// <summary>
/// 月份
/// </summary>
[JsonPropertyName("cMonth")]
public int Month { get; set; }
/// <summary>
/// 天
/// </summary>
[JsonPropertyName("cDay")]
public uint Day { get; set; }
/// <summary>
/// 生肖
/// </summary>
public string? Animal { get; set; }
public string? YearCn { get; set; }
public string? MonthCn { get; set; }
public string? DayCn { get; set; }
public string? GzYear { get; set; }
public string? GzMonth { get; set; }
public string? GzDay { get; set; }
public bool IsToday { get; set; }
public bool IsLeap { get; set; }
[JsonPropertyName("nWeek")]
public uint Week { get; set; }
public string? NcWeek { get; set; }
public bool IsTerm { get; set; }
/// <summary>
/// 节气
/// </summary>
public string? Term { get; set; }
}
public class History
{
public string? Event { get; set; }
}
public class News
{
public string? Title { get; set; }
public string? Url { get; set; }
public string? Category { get; set; }
}
public class Phrase
{
[JsonPropertyName("phrase")]
public string? PhraseName { get; set; }
public string? Explain { get; set; }
public string? From { get; set; }
public string? Simple { get; set; }
public string? Pinyin { get; set; }
}
public class Sentence
{
public bool Wrong { get; set; }
public string? Author { get; set; }
[JsonPropertyName("sentence")]
public string? SentenceContent { get; set; }
}
public class Poem
{
public string[]? Content { get; set; }
public string? Type { get; set; }
public string? Title { get; set; }
public string? Author { get; set; }
}
public class Weather
{
public string? City { get; set; }
public string? WeatherOf { get; set; }
public WeatherDetail? Detail { get; set; }
}
public class WeatherDetail
{
public DateTime Date { get; set; }
[JsonPropertyName("text_day")]
public string? TextDay { get; set; }
[JsonPropertyName("code_day")]
public string? CodeDay { get; set; }
[JsonPropertyName("text_night")]
public string? TextNight { get; set; }
[JsonPropertyName("code_night")]
public string? CodeNight { get; set; }
public string? High { get; set; }
public string? Low { get; set; }
public string? Rainfall { get; set; }
/// <summary>
/// 降水概率
/// </summary>
public string? Precip { get; set; }
[JsonPropertyName("wind_direction")]
public string? WindDirection { get; set; }
[JsonPropertyName("wind_direction_degree")]
public string? WindDirectionDegree { get; set; }
[JsonPropertyName("wind_speed")]
public string? WindSpeed { get; set; }
[JsonPropertyName("wind_scale")]
public string? WindScale { get; set; }
/// <summary>
/// 湿度
/// </summary>
public string? Humidity { get; set; }
}
⚠⚠ 以下内容为AI分析的结果,请根据实际情况进行判断。
这是一个用于处理新闻响应数据的C#模型类文件,主要用于JSON序列化和反序列化。以下是详细解释:
文件结构概述
这个文件定义了一系列用于表示"今日新闻"API响应的数据模型类,涵盖了日历、历史事件、新闻、短语、句子、诗词和天气等多个方面的信息。
主要类分析
1. NewsResponse(响应根类)
public class NewsResponse
{
public int Code { get; set; } // 响应状态码
public TodayNewsModel? Data { get; set; } // 主要数据内容
public string? Message { get; set; } // 响应消息
}
这是API响应的根类,采用标准的API响应格式(状态码+数据+消息)。
2. TodayNewsModel(主数据模型)
包含了今日新闻的所有相关信息:
Calendar- 日历信息HistoryList- 历史事件列表NewsList- 新闻列表Phrase- 成语/短语Sentence- 句子Poem- 诗词Weather- 天气信息
3. Calendar(日历类)
提供详细的日期信息:
- 公历日期(年、月、日)
- 农历信息(生肖、中文日期、干支等)
- 星期信息
- 节气信息
- 闰年标识等
特点:使用了JsonPropertyName特性来映射JSON属性名,如cYear、cMonth等。
4. 其他数据模型
- History - 历史事件信息
- News - 新闻信息(标题、链接、分类)
- Phrase - 成语信息(名称、解释、出处、拼音等)
- Sentence - 句子信息(内容、作者)
- Poem - 诗词信息(内容数组、类型、标题、作者)
- Weather/WeatherDetail - 天气信息(城市、详细天气数据)
设计特点
1. JSON序列化支持
- 使用
System.Text.Json.Serialization命名空间 - 通过
JsonPropertyName特性处理属性名映射 - 支持.NET的现代JSON处理
2. 可空类型使用
- 大量使用
string?、List<T>?等可空引用类型 - 提高了代码的null安全性
3. 数据完整性
- 涵盖了完整的天气信息(白天/夜间、温度、风向、湿度等)
- 提供了丰富的日历信息(公历+农历)
4. 良好的代码组织
- 使用XML文档注释提供属性说明
- 合理的类结构分层
- 清晰的命名约定
使用场景
这个模型类适用于:
- 新闻聚合应用
- 日历/万年历应用
- 天气预报功能
- 文化内容展示(诗词、成语等)
- 综合信息Dashboard
这种设计使得应用能够方便地处理包含多种信息类型的复合API响应数据。
AI 正在分析代码…
评论加载中...