簡體   English   中英

將復雜的json轉換為c#類

[英]convert complex json to c# class

我一直在嘗試使用來自此網站和其他網站的類似問題的幫助,將以下json數據反序列化為2天,並且可能會使腦死亡。

我有這個json數據(長度道歉)並且我正在嘗試,作為開始在'Values'數組中獲得'value'數字: -

{
  "metadata": {
    "columnGrouping": [
      "area",
      "metricType",
      "period",
      "valueType"
    ],
    "rowGrouping": []
  },
  "columns": [
    {
      "area": {
        "identifier": "E31000040",
        "label": "Gtr Manchester Fire",
        "altLabel": "Gtr Manchester Fire",
        "isSummary": false
      },
      "metricType": {
        "identifier": "948",
        "label": "Accidental dwelling fires",
        "altLabel": "Accidental dwelling fires",
        "isSummary": false
      },
      "period": {
        "identifier": "fq_Q1_2013_14",
        "label": "2013/14 Q1",
        "altLabel": "2013/14 Q1",
        "isSummary": false
      },
      "valueType": {
        "identifier": "raw",
        "label": "Raw value",
        "isSummary": false
      }
    },
    {
      "area": {
        "identifier": "E31000040",
        "label": "Gtr Manchester Fire",
        "altLabel": "Gtr Manchester Fire",
        "isSummary": false
      },
      "metricType": {
        "identifier": "948",
        "label": "Accidental dwelling fires",
        "altLabel": "Accidental dwelling fires",
        "isSummary": false
      },
      "period": {
        "identifier": "fq_Q2_2013_14",
        "label": "2013/14 Q2",
        "altLabel": "2013/14 Q2",
        "isSummary": false
      },
      "valueType": {
        "identifier": "raw",
        "label": "Raw value",
        "isSummary": false
      }
    },
    {
      "area": {
        "identifier": "E31000040",
        "label": "Gtr Manchester Fire",
        "altLabel": "Gtr Manchester Fire",
        "isSummary": false
      },
      "metricType": {
        "identifier": "948",
        "label": "Accidental dwelling fires",
        "altLabel": "Accidental dwelling fires",
        "isSummary": false
      },
      "period": {
        "identifier": "fq_Q3_2013_14",
        "label": "2013/14 Q3",
        "altLabel": "2013/14 Q3",
        "isSummary": false
      },
      "valueType": {
        "identifier": "raw",
        "label": "Raw value",
        "isSummary": false
      }
    },
    {
      "area": {
        "identifier": "E31000040",
        "label": "Gtr Manchester Fire",
        "altLabel": "Gtr Manchester Fire",
        "isSummary": false
      },
      "metricType": {
        "identifier": "948",
        "label": "Accidental dwelling fires",
        "altLabel": "Accidental dwelling fires",
        "isSummary": false
      },
      "period": {
        "identifier": "fq_Q4_2013_14",
        "label": "2013/14 Q4",
        "altLabel": "2013/14 Q4",
        "isSummary": false
      },
      "valueType": {
        "identifier": "raw",
        "label": "Raw value",
        "isSummary": false
      }
    }
  ],
  "rows": [
    {
      "values": [
        {
          "source": 515.0,
          "value": 515.0,
          "formatted": "515",
          "format": "#,##0",
          "publicationStatus": "Published"
        },
        {
          "source": 264.0,
          "value": 264.0,
          "formatted": "264",
          "format": "#,##0",
          "publicationStatus": "Published"
        },
        {
          "source": 254.0,
          "value": 254.0,
          "formatted": "254",
          "format": "#,##0",
          "publicationStatus": "Published"
        },
        {
          "source": 455.0,
          "value": 455.0,
          "formatted": "455",
          "format": "#,##0",
          "publicationStatus": "Published"
        }
      ]
    }
  ]
}

我使用http://json2csharp.com/創建了類,並嘗試過以下方法: -

RootObject ro = JsonConvert.DeserializeObject<RootObject>(json_data);

Value [] vo = JsonConvert.DeserializeObject<Value[]>(json_data);

dynamic result = JsonConvert.DeserializeObject(json_data);

JavaScriptSerializer jss = new JavaScriptSerializer();
Value [] thisval = jss.Deserialize<Value[]>(json_data);

其中包括。 什么是將他的信息提取到類中的正確方法,然后我可以處理它們。 一旦反序列化調用數據的示例將是有幫助的。 我的主要課程是

public class Value
{
    public double source { get; set; }
    public double value { get; set; }
    public string formatted { get; set; }
    public string format { get; set; }
    public string publicationStatus { get; set; }
}

public class Row
{
    public List<Value> values { get; set; }
}

public class RootObject
{
    public Metadata metadata { get; set; }
    public List<Column> columns { get; set; }
    public List<Row> rows { get; set; }
}

這是一個有效的dotNet小提琴,可以對Values列表進行反序列化。 https://dotnetfiddle.net/7P2em6

加載小提琴時等待幾秒鍾,並在控制台窗口中注意輸出。 代碼應該是不言自明的,但如果您需要幫助,請告訴我。

我還在下面粘貼了它,以防萬一dotNetFiddle不可用。

控制台輸出:

將復雜的json反序列化為c#類

我使用http://json2csharp.com/從JSON字符串生成類。

我認為您的問題可能是ValuesRow對象中的List ,而Row對象又是RootObjectList 換句話說, Value s作為List存儲在List

完整的代碼清單

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

    // SO Question: http://stackoverflow.com/questions/27132887/
    // This (my) Answer: 
    // Author: Shiva Manjunath
    // SO Profile: http://stackoverflow.com/users/325521/shiva
public class Program
{   
    public static void Main()
    {

       string jsonString = @"{
  ""metadata"": {
    ""columnGrouping"": [
      ""area"",
      ""metricType"",
      ""period"",
      ""valueType""
    ],
    ""rowGrouping"": []
  },
  ""columns"": [
    {
      ""area"": {
        ""identifier"": ""E31000040"",
        ""label"": ""Gtr Manchester Fire"",
        ""altLabel"": ""Gtr Manchester Fire"",
        ""isSummary"": false
      },
      ""metricType"": {
        ""identifier"": ""948"",
        ""label"": ""Accidental dwelling fires"",
        ""altLabel"": ""Accidental dwelling fires"",
        ""isSummary"": false
      },
      ""period"": {
        ""identifier"": ""fq_Q1_2013_14"",
        ""label"": ""2013/14 Q1"",
        ""altLabel"": ""2013/14 Q1"",
        ""isSummary"": false
      },
      ""valueType"": {
        ""identifier"": ""raw"",
        ""label"": ""Raw value"",
        ""isSummary"": false
      }
    },
    {
      ""area"": {
        ""identifier"": ""E31000040"",
        ""label"": ""Gtr Manchester Fire"",
        ""altLabel"": ""Gtr Manchester Fire"",
        ""isSummary"": false
      },
      ""metricType"": {
        ""identifier"": ""948"",
        ""label"": ""Accidental dwelling fires"",
        ""altLabel"": ""Accidental dwelling fires"",
        ""isSummary"": false
      },
      ""period"": {
        ""identifier"": ""fq_Q2_2013_14"",
        ""label"": ""2013/14 Q2"",
        ""altLabel"": ""2013/14 Q2"",
        ""isSummary"": false
      },
      ""valueType"": {
        ""identifier"": ""raw"",
        ""label"": ""Raw value"",
        ""isSummary"": false
      }
    },
    {
      ""area"": {
        ""identifier"": ""E31000040"",
        ""label"": ""Gtr Manchester Fire"",
        ""altLabel"": ""Gtr Manchester Fire"",
        ""isSummary"": false
      },
      ""metricType"": {
        ""identifier"": ""948"",
        ""label"": ""Accidental dwelling fires"",
        ""altLabel"": ""Accidental dwelling fires"",
        ""isSummary"": false
      },
      ""period"": {
        ""identifier"": ""fq_Q3_2013_14"",
        ""label"": ""2013/14 Q3"",
        ""altLabel"": ""2013/14 Q3"",
        ""isSummary"": false
      },
      ""valueType"": {
        ""identifier"": ""raw"",
        ""label"": ""Raw value"",
        ""isSummary"": false
      }
    },
    {
      ""area"": {
        ""identifier"": ""E31000040"",
        ""label"": ""Gtr Manchester Fire"",
        ""altLabel"": ""Gtr Manchester Fire"",
        ""isSummary"": false
      },
      ""metricType"": {
        ""identifier"": ""948"",
        ""label"": ""Accidental dwelling fires"",
        ""altLabel"": ""Accidental dwelling fires"",
        ""isSummary"": false
      },
      ""period"": {
        ""identifier"": ""fq_Q4_2013_14"",
        ""label"": ""2013/14 Q4"",
        ""altLabel"": ""2013/14 Q4"",
        ""isSummary"": false
      },
      ""valueType"": {
        ""identifier"": ""raw"",
        ""label"": ""Raw value"",
        ""isSummary"": false
      }
    }
  ],
  ""rows"": [
    {
      ""values"": [
        {
          ""source"": 515.0,
          ""value"": 515.0,
          ""formatted"": ""515"",
          ""format"": ""#,##0"",
          ""publicationStatus"": ""Published""
        },
        {
          ""source"": 264.0,
          ""value"": 264.0,
          ""formatted"": ""264"",
          ""format"": ""#,##0"",
          ""publicationStatus"": ""Published""
        },
        {
          ""source"": 254.0,
          ""value"": 254.0,
          ""formatted"": ""254"",
          ""format"": ""#,##0"",
          ""publicationStatus"": ""Published""
        },
        {
          ""source"": 455.0,
          ""value"": 455.0,
          ""formatted"": ""455"",
          ""format"": ""#,##0"",
          ""publicationStatus"": ""Published""
        }
      ]
    }
  ]
}";

      Console.WriteLine("Begin JSON Deserialization\n");

      var rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
      var rows = rootObject.rows; 
      int rowCounter = 1;
      foreach (Row oneRow in rows)
      {
          Console.WriteLine("Row: " + rowCounter);
          int valueCounter = 1;
          foreach(Value oneValue in oneRow.values)
          {
            Console.WriteLine("    Value: " + valueCounter);              
            Console.WriteLine("        source: " + oneValue.source);
            Console.WriteLine("        value: " + oneValue.value);
            Console.WriteLine("        formatted: " + oneValue.formatted);
            Console.WriteLine("        publicationStatus: " + oneValue.publicationStatus);                
            valueCounter++;
          }
          rowCounter++;
      }

      Console.WriteLine("\nEnd JSON Deserialization");

}
}

public class Metadata
{
    public List<string> columnGrouping { get; set; }
}

public class Area
{
    public string identifier { get; set; }
    public string label { get; set; }
    public string altLabel { get; set; }
    public bool isSummary { get; set; }
}

public class MetricType
{
    public string identifier { get; set; }
    public string label { get; set; }
    public string altLabel { get; set; }
    public bool isSummary { get; set; }
}

public class Period
{
    public string identifier { get; set; }
    public string label { get; set; }
    public string altLabel { get; set; }
    public bool isSummary { get; set; }
}

public class ValueType
{
    public string identifier { get; set; }
    public string label { get; set; }
    public bool isSummary { get; set; }
}

public class Column
{
    public Area area { get; set; }
    public MetricType metricType { get; set; }
    public Period period { get; set; }
    public ValueType valueType { get; set; }
}

public class Value
{
    public double source { get; set; }
    public double value { get; set; }
    public string formatted { get; set; }
    public string format { get; set; }
    public string publicationStatus { get; set; }
}

public class Row
{
    public List<Value> values { get; set; }
}

public class RootObject
{
    public Metadata metadata { get; set; }
    public List<Column> columns { get; set; }
    public List<Row> rows { get; set; }
}

注意:對於columns對象,您不需要單獨的字段類(json2csharp.com類生成器將默認為該類)。 您可以將值存儲在字典類的columns對象中(如果您知道它們的名稱將是唯一的)。 對於它的實現(不同的JSON字符串,但相同的json模式類型,原則),請看這個小提琴: https//dotnetfiddle.net/7bFcNM

使用json到C#生成器,如JSON C# Class Generator

如果重命名屬性並使它們與json的名稱匹配,則可以使用Lists替換數組並刪除JsonProperty屬性。

這是輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM