【发布时间】:2021-10-24 22:29:58
【问题描述】:
我正在做一个 .net 项目。我需要将 json 模型(已动态更改数字节点名称)转换为 csharp 类。第二个节点的名称是动态变化的,这就是为什么我不能像这样写一个 jsonproperty:[JsonProperty("353503")]。我没有看到这样的 json 模型。我尝试了很多东西,但无法实现。我该怎么办?
{
"standings": {
"353493": {
"id": "353493",
"name": "root name",
"n": "0",
"ut": "2021-08-23T20:54:12+00:00",
"standing_participants": {
"4273616": {
"id": "4273616",
"n": "14",
"ut": "2021-08-23T20:54:11+00:00",
"rank": "1",
"standing_data": [
{
"id": "146906199",
"standing_type_paramFK": "1",
"value": "6",
"code": "points",
"n": "2",
"ut": "2021-08-23T20:54:11+00:00",
"sub_param": ""
},
{
"id": "146906200",
"standing_type_paramFK": "2",
"value": "8",
"code": "goalsfor",
"n": "2",
"ut": "2021-08-23T20:54:11+00:00",
"sub_param": ""
}
]
},
"4273617": {
"id": "4273617",
"n": "14",
"ut": "2021-08-23T20:54:11+00:00",
"rank": "2",
"standing_data": [
{
"id": "146906198",
"standing_type_paramFK": "1",
"value": "6",
"code": "points",
"n": "2",
"ut": "2021-08-23T20:54:11+00:00",
"sub_param": ""
},
{
"id": "146906201",
"standing_type_paramFK": "2",
"value": "8",
"code": "goalsfor",
"n": "2",
"ut": "2021-08-23T20:54:11+00:00",
"sub_param": ""
}
]
}
}
}
}
}
当我尝试这个时,它可以工作
public partial class SampleStandingModel
{
[JsonProperty("standings")]
public Standings Standings { get; set; }
}
public partial class Standings
{
[JsonProperty("353493")]
public Standing Standing { get; set; }
}
但是当我删除 [JsonProperty("353493")] 行时,它不起作用
public partial class SampleStandingModel
{
[JsonProperty("standings")]
public Standings Standings { get; set; }
}
public partial class Standings
{
public Standing Standing { get; set; }
//or this:
// public Dictionary<int, Standing> Standing { get; set; }
}
我的 json 解析代码如下:
var deserializedObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
我的问题的重点,第二个节点(站立-> 353493)名称是数字和动态的。
我之前标记的问题已回答,但没有相同的问题。较早的问题与动态类型或对象有关。这个关于数字和动态节点名称的问题。两天没找到解决办法。
【问题讨论】: