2014-11-25 3 views
4

Я пытаюсь десериализовать следующие данные json в классы в течение 2 дней, используя справку из похожих вопросов на этом и других сайтах, и, возможно, это может привести к смерти мозга.преобразовать комплекс json в C# class

У меня есть этот JSon данные (Извиняюсь за длину), и я пытаюсь, как начать, чтобы получить «значение» число в массиве «ценности»: -

{ 
    "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; } 
} 
+0

Ну 'RootObject ро = JsonConvert.DeserializeObject (json_data)' работает для меня (используя пустые классы для 'метаданных 'и' Column') – DavidG

ответ

5

Вот рабочий Dotnet скрипку, что десериализует Values List. https://dotnetfiddle.net/7P2em6

Подождите несколько секунд, когда вы загрузите скрипку и обратите внимание на выход в окне консоли. Код должен быть понятным, но дайте мне знать, если вам нужна помощь.

Я также наклеил его ниже для полноты в случае, если dotNetFiddle недоступен.

Консоль вывода:

deserialize complex json to c# class

http://json2csharp.com/ Я использовал для создания классов из строки JSON.

Я думаю, что ваш вопрос может быть, что Values является List внутри объекта, который, в свою очередь, является List внутри RootObject. Другими словами, 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

+0

Спасибо за четкий ответ с рабочим примером. Еще один вопрос. Со списком в списке, как бы я ссылался только на одно значение, например: ** var test = rootObject.rows [0] .values ​​[1] .value ** – Porkster

+0

Да, это правильно. Вы должны использовать 'rows [0] .values ​​[1] .value' для доступа к 1-му элементу в списке' rows' (индекс начинается с '0') и 2-й элемент' Value' в списке 'values' внутри 'rows' list. Я обновил скрипт, связанный в моем ответе, чтобы распечатать это тоже, поэтому проверьте это! – Shiva

2

Использование JSON для генератора C# как JSON C# Class Generator.

Вы можете заменить массивы списками и отбросить атрибуты JsonProperty, если вы переименуете свойства и сделаете их соответствующими имени json.

Вот результат:

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

Спасибо за ответ, я думаю, что мне это обязательно понадобится для дальнейшего программирования. – Porkster