1

I have a JSON like this

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

And correpsonding c# code to this json like this

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

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

The problem is that i do not know how to convert actions JSON array filed like above code.Some help?

3
  • Why not try representing actions with a List, Array, or IEnumerable in C#? Commented Mar 6, 2018 at 12:24
  • Could be relevant here: Easiest way to parse JSON response. Commented Mar 6, 2018 at 12:24
  • Share complete JSON. Commented Mar 6, 2018 at 12:25

1 Answer 1

4

First, you need to create a corresponding class which will represent object in actions array

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; }
}

then you need to create List<Action> property inside your root class

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 ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this was exactly what i was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.