convert complex json to c# class

前端 未结 2 611
天涯浪人
天涯浪人 2021-01-13 16:49

I have been trying to deserialize the following json data into classes for 2 days using the help from similar questions on this and other sites, and might possibly be going

2条回答
  •  悲&欢浪女
    2021-01-13 17:21

    Use a json to C# generator like JSON C# Class Generator.

    You can replace the arrays with Lists and drop the JsonProperty attributes if you rename the properties and make them match the json's name.

    Here's the output:

    internal class Test
    {
        [JsonProperty("metadata")]
        public Metadata Metadata { get; set; }
    
        [JsonProperty("columns")]
        public Column[] Columns { get; set; }
    
        [JsonProperty("rows")]
        public Row[] Rows { get; set; }
    }
    
    internal class Metadata
    {    
        [JsonProperty("columnGrouping")]
        public string[] ColumnGrouping { get; set; }
    
        [JsonProperty("rowGrouping")]
        public object[] RowGrouping { get; set; }
    }
    
    internal class Area
    {   
        [JsonProperty("identifier")]
        public string Identifier { get; set; }
    
        [JsonProperty("label")]
        public string Label { get; set; }
    
        [JsonProperty("altLabel")]
        public string AltLabel { get; set; }
    
        [JsonProperty("isSummary")]
        public bool IsSummary { get; set; }
    }
    
    internal class MetricType
    {   
        [JsonProperty("identifier")]
        public string Identifier { get; set; }
    
        [JsonProperty("label")]
        public string Label { get; set; }
    
        [JsonProperty("altLabel")]
        public string AltLabel { get; set; }
    
        [JsonProperty("isSummary")]
        public bool IsSummary { get; set; }
    }
    
    internal class Period
    {   
        [JsonProperty("identifier")]
        public string Identifier { get; set; }
    
        [JsonProperty("label")]
        public string Label { get; set; }
    
        [JsonProperty("altLabel")]
        public string AltLabel { get; set; }
    
        [JsonProperty("isSummary")]
        public bool IsSummary { get; set; }
    }
    
    internal class ValueType
    {   
        [JsonProperty("identifier")]
        public string Identifier { get; set; }
    
        [JsonProperty("label")]
        public string Label { get; set; }
    
        [JsonProperty("isSummary")]
        public bool IsSummary { get; set; }
    }
    
    internal class Column
    {  
        [JsonProperty("area")]
        public Area Area { get; set; }
    
        [JsonProperty("metricType")]
        public MetricType MetricType { get; set; }
    
        [JsonProperty("period")]
        public Period Period { get; set; }
    
        [JsonProperty("valueType")]
        public ValueType ValueType { get; set; }
    }
    
    internal class Value
    { 
        [JsonProperty("source")]
        public double Source { get; set; }
    
        [JsonProperty("value")]
        public double Value { get; set; }
    
        [JsonProperty("formatted")]
        public string Formatted { get; set; }
    
        [JsonProperty("format")]
        public string Format { get; set; }
    
        [JsonProperty("publicationStatus")]
        public string PublicationStatus { get; set; }
    }
    
    internal class Row
    { 
        [JsonProperty("values")]
        public Value[] Values { get; set; }
    }
    

提交回复
热议问题