如何在c#字段中转换json数组字段

问题描述 投票:1回答:1

我有这样的JSON

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

并且像这样对这个json的c#代码进行相应的处理

        [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个回答
4
投票

首先,您需要创建一个对应的类,它将表示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; }
}

那么你需要在你的根类中创建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 ...
}
© www.soinside.com 2019 - 2024. All rights reserved.