c# - 我可以映射一个 JSON 元素“@somename”吗?
问题描述
我的目标是带有 winforms 应用程序的 .net framework 4.7。我首先关注这篇文章https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/console-webapiclient所以我正在使用 DataContractJsonSerializer。我正在尝试了解返回 JSON 的 REST 接口 -
{"Resource":
{"@attributes":
{"name":"Asset",
"resourceId":"Asset",
"type":"Resource"
}
}
}
我使用 netwonsoft.json 12.0.2 将 JSON 粘贴为类。它忽略“@”字符,并在“资源”类中创建一个“属性”类型的成员“属性”。
当 DataContractJsonSerializer 尝试反序列化 JSON 时,它会跳过 @attribute 元素,我认为是因为它与类名不匹配。
有没有办法将元素 @attributes 映射到我的属性成员/类?
我尝试在 Resource 类的属性成员上添加 [DataMember(Name = "@attributes")] 并在 Attributes 类上添加 [DataContract(Name = "@attributes")] 但仍然似乎跳过了元素(属性资源的成员为空)。
解决方案
是的,使用JsonProperty
public class MyClass
{
[JsonProperty("@attributes")]
public string attributes { get; set; }
}
推荐阅读
- service - 为 CRM SRS 数据连接器创建空白虚拟 Windows 服务
- r - 计算微阵列中的全局中值归一化
- python - 如何正确使用 Python 请求?
- django - 为什么安装 zappa 时出现“非法多字节序列”错误?
- javascript - 在 Javascript 中调整图像大小
- python - 在第一个列表中的单词中搜索一个字母并在 python 中的第二个列表中替换为一个真实的单词
- firebase - Flutter如何获取firestore子集合ID
- java - 我可以在这里做什么来检测这两种方法中的重复项
- python - 如何使我的数据帧的 Ngram 的字典以一些字符串 Python 开头
- python - Dask 数据框中指定的列太多