[英]Serialize JSON property name with comma in its name?
I am trying to retrieve JSON data from an API, but one of the property names comes as @data.context
.我正在尝试从 API 中检索 JSON 数据,但其中一个属性名称为
@data.context
。 It is not a nested property.它不是嵌套属性。 I've tested it with ExpandoObject as well, it is exactly like that.
我也用 ExpandoObject 测试过,完全一样。 Normally in C # I would make a data model like
通常在 C # 我会做一个数据 model 像
public class Data
{
public string @odata.context { get ; set; }
}
But this doesn't work, as C# doesn't let me have a comma in the variable name, nor have quotes around it.但这不起作用,因为 C# 不允许我在变量名中有逗号,也没有引号。 The
@
sign is already there @
符号已经存在
The JSON is as follows: this property that contains a link and then another one that contains a list of objects. JSON 如下:这个属性包含一个链接,然后另一个属性包含一个对象列表。
{
"@odata.context": "some link here",
"list" [ {}, {}
]
}
The list of objects do not give me any trouble, only the first property.对象列表不会给我带来任何麻烦,只有第一个属性。
You can use JsonPropertyName attribute to map the json to the property eg:您可以将 JsonPropertyName 属性用于 map 和 json 的属性,例如:
[JsonPropertyName("@odata.context")]
public string DataContext { get ; set; }
You might be consuming a poorly designed API.您可能正在使用设计不佳的 API。 There are API specifications that tells how to structure and name JSON keys.
有 API 规范说明如何构造和命名 JSON 密钥。 One way you can accomplish is
您可以完成的一种方法是
1 Fetch JSON response from API 1 从 API 获取 JSON 响应
2 Replace "@data.context" with "Context" or something similar in JSON string 2 将“@data.context”替换为“Context”或 JSON 字符串中的类似内容
3 Create class with property 3 使用属性创建 class
public class Data
{
public string Context { get ; set; }
}
4 Deserialise it 4 反序列化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.