c# - 使用 JSON 数组发布请求 - 无法以正确的格式发送
问题描述
我有以下需要发布到 API 的 JSON:
{
"arch": {
"id": “TrackingCode”
},
"nails": [{
"name": "John"
}],
"token": 'RandomCode'
}
所以我这样定义数据:
public class arch
{
[JsonProperty("id")]
public string id { get; set; }
}
public class nails
{
[JsonProperty("name")]
public string[] name { get; set; }
}
public class Parameter
{
[JsonProperty("arch")]
public arch arch { get; set; }
[JsonProperty("nails")]
public nails nails{ get; set; }
[JsonProperty("token")]
public string token { get; set; }
}
这就是我在序列化它之前初始化 JSON 的方式:
Parameter json = new Parameter
{
arch = new arch
{
id = TrackingId
},
nails = new nails
{
name = "John"
}
token = "randomstuff"
};
但是有一个涉及“名称”字段的语法/格式错误,不允许编译。显然是那个元素的数组结构。我在做什么错误的语法?
解决方案
在您的参数对象中更改nails nails
为nails[]
或IEnumerable<nail> nails
。您的 json 没有按照您的意愿出现的原因是因为 nails 是一个对象,因此单个实体与数组是您想要的多个实体
推荐阅读
- django - 使用“同源”引荐来源网址策略访问 Amazon S3 上的 Django 静态文件
- python - R 的 plm 包等效于 python/pyspark
- java - Java代码无法将二进制转换为十六进制(也可以包含十进制值)
- java - @ManyToOne @OneToMany 映射,外键为空
- python - 我们可以在不显式覆盖的情况下使用 python 关键字作为变量名吗?
- java - 无法使用 Java Swing 添加图像
- python - 使用 enumerate 标记绘图上的点,每个点使用不同的字母
- automation - 如何使用 Vividus 工具测试下载文件
- java - new File(String url, String child) 修改 url
- ansible - 在多个主机名上运行时,使用什么代替 lineinfile 通过 ansible 打印文件中的重复值