skip to Main Content

I’ve searched "all over" the internet and can’t find a solution to my problem.

I want to deserialize the following string

{"retention": [
{ "kinds": [0, 1, [5, 7], [40, 49]], "time": 3600, "count": 10000 }
]}

kinds[0] -> integer
kinds[1] -> integer
kinds[2] -> integer range

public class RelayRetention
{
public override string ToString()
    {
        string text = Environment.NewLine;

        return text;
    }

    //[JsonPropertyName("kinds")] public int[][]? KindsArrys { get; set; }
    [JsonPropertyName("kinds")] public int[]? Kinds { get; set; }
    [JsonPropertyName("time")] public int? Time { get; set; }
    [JsonPropertyName("count")] public int? Count { get; set; }
}
string s = "{"retention": [
    { "kinds": [0, 1, [5, 7], [40, 49]], "time": 3600, "count": 10000 }";
]}
RelayRetention r = JsonSerializer.Deserialize<RelayRetention>(s);

2

Answers


  1. Chosen as BEST ANSWER
    public class RelayRetention
    {
        [JsonPropertyName("kinds")] public List<object>? Kinds { get; set; }
        [JsonPropertyName("time")] public int? Time { get; set; }
        [JsonPropertyName("count")] public int? Count { get; set; }
    }
    
    //kinds[0] -> integer type 
    //kinds[1] -> integer type 
    //kinds[2] -> integer range type
    //kinds[3] -> integer range type
    
    string str = "{[{"kinds": [0, 1, [5, 7], [40, 49]], "time": 3600, "count": 10000 }]}";
    RelayRetention r = JsonSerializer.Deserialize<RelayRetention>(str);
    

  2. you have to fix your code and class

    List<RelayRetention> lr =JsonObject.Parse(json)["retention"]
                                        .Deserialize<List<RelayRetention>>();
    public class RelayRetention
    {
        public override string ToString()
        {
            string text = Environment.NewLine;
    
            return text;
        }
        
        [JsonPropertyName("kinds")] public List<object>? Kinds { get; set; }
        [JsonPropertyName("time")] public int? Time { get; set; }
        [JsonPropertyName("count")] public int? Count { get; set; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search