gpt4 book ai didi

c# - 将复杂的json转换为c#类

转载 作者:太空狗 更新时间:2023-10-30 00:40:31 25 4
gpt4 key购买 nike

在 2 天的时间里,我一直在尝试使用此站点和其他站点上类似问题的帮助将以下 json 数据反序列化为类,并且可能会脑死亡。

我有这个 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);

等等。将他的信息提取到类(class)中的正确方法是什么,这样我就可以处理它们了。反序列化后调用数据的示例会有所帮助。我的主要类(class)是

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

最佳答案

这是一个可以反序列化 Values 列表的工作 dotNet Fiddle。 https://dotnetfiddle.net/7P2em6

加载 fiddle 时请等待几秒钟,然后注意控制台窗口中的输出。代码应该是不言自明的,但如果您需要帮助,请告诉我。

为了完整起见,我还在下面粘贴了它,以防 dotNetFiddle 不可用。

控制台输出:

deserialize complex json to c# class

我用了http://json2csharp.com/从 JSON 字符串生成类。

我认为您的问题可能是 ValuesRow 对象中的 List,而后者又是 ListRootObject 中。换句话说,Value 存储为 List 中的 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 模式类型,原理)请参见这个 fiddle :https://dotnetfiddle.net/7bFcNM

关于c# - 将复杂的json转换为c#类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27132887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com