c# - 从蛇案到骆驼案的对象序列化
问题描述
我有一个对象,该对象的字段为小写并带有下划线
public class Project
{
public int project_id { get; set; }
public long account_id { get; set; }
public long user_id { get; set; }
public string name { get; set; }
public string description { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}
当 GET、POST 等 API 命中 API 时,我需要在骆驼情况下进行序列化。我该怎么做?
json就像:
{
"project_id": 10,
"account_id": 10,
"user_id": 10,
"name": "string",
"description": "string",
"created_at": "Date",
"updated_at": "Date"
}
我想要这种格式:
{
"projectId": 10,
"accountId": 10,
"userId": 10,
"name": "string",
"description": "string",
"createdAt": "Date",
"updatedAt": "Date"
}
解决方案
尝试使用 NewtonSoft 的JsonProperty 属性来注释您的属性。
using Newtonsoft.Json;
public class Project
{
[JsonProperty("projectId")]
public int project_id { get; set; }
[JsonProperty("accountId")]
public long account_id { get; set; }
// ..
// ..
// ..
}
推荐阅读
- php - PHP:括号交互的preg_replace问题')'
- javascript - 在没有 require() 输出的情况下,无法使用 webpack 和 Babel 为 Web 创建缩小的 JavaScript 库
- jhipster - 无法将网关或微服务连接到 JHipster Registry。Jhipster v5.8.2
- javascript - 向下滚动时调整导航栏徽标的大小,Javascript 不起作用
- python - 接收 ValueError: x 和 y 的 x 和 y 值的大小必须相同。任何帮助,将不胜感激
- android - 我需要多个用于 Google 地图的 API 密钥吗?
- python-3.x - 如何在 Python 3.7 中创建一个通用的 id 类
- arduino - Arduino IR 控制的光衰问题
- makefile - 是否可以将变量从子 makefile 传递到顶级 makefile?
- javascript - 物化 CSS 日期选择器无法正常工作