【问题标题】:How to convert json array field in c# field如何在c#字段中转换json数组字段
【发布时间】:2018-03-06 12:21:12
【问题描述】:

我有一个这样的 JSON

"type" : "info", 
"gameid" : "info", 
"userid" : "info", 
"actions" : 
[ 
  { 
   "actid" : "info", 
   "type" : "info", 
   "amount" : "info", 
   "timestamp" : "info" 
  },                                      
], 
"i_gamedesc" : "{SystemID}:{GameType}", 
"hmac" : "..." 

并像这样将 c# 代码对应到这个 json

        [JsonProperty(PropertyName ="gameid")]
        public int gameId { get; set; }

        [JsonProperty(PropertyName = "userid")]
        public int userId { get; set; }

问题是我不知道如何转换actions JSON 数组,如上面的代码。一些帮助?

【问题讨论】:

标签: c# .net arrays json json.net


【解决方案1】:

首先,你需要创建一个对应的类来表示actions数组中的对象

public class Action
{
   [JsonProperty(PropertyName = "actid")]
   public string ActId { get; set; }

   public string Type { get; set; }

   public string Amount { get; set; }

   public string Timestamp { get; set; }
}

那么你需要在你的 root 类中创建List<Action> 属性

public class Root
{
    [JsonProperty(PropertyName ="gameid")]
    public int GameId { get; set; }

    [JsonProperty(PropertyName = "userid")]
    public int UserId { get; set; }

    [JsonProperty(PropertyName = "actions")]
    public List<Action> Actions { get; set; }

    ... other properties ...
}

【讨论】:

  • 谢谢,这正是我想要的。
猜你喜欢
  • 2021-05-15
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2020-10-04
  • 2022-12-03
  • 1970-01-01
相关资源
最近更新 更多