复杂的JSON转换为C#类 [英] convert complex json to c# class
问题描述
我一直在尝试反序列化下面的JSON数据转换成类利用这和其他网站类似的问题的帮助下2天,有可能会被持续脑死亡。
我有这样的JSON数据(道歉长度),我试图,作为开始,以获得价值数组中的价值数: -
{
元数据:{
columnGrouping:[
区,
metricType,
时期,
值类型
],
rowGrouping:[]
},
柱:[
{
区:{
标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary :虚假
},
metricType:{
标识:948,
标签:意外住宅火灾,
altLabel :意外住宅火灾,
isSummary:虚假
},
时期:{
标识:fq_Q1_2013_14,
标签:2013/14 Q1,
altLabel:2013/14 Q1,
isSummary:虚假
},
值类型:{
标识:原始,
标签:原始值,
isSummary:虚假
}
},
{
区:{
标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType:{
标识:948,
标签:意外住宅火灾,
altLabel:意外住宅火灾,
isSummary:虚假
},
时期:{
标识:fq_Q2_2013_14,
标签:2013/14 Q2,
altLabel:2013/14 Q2,
isSummary:虚假
},
值类型: {
标识:原始,
标签:原始值,
isSummary:虚假
}
},
{
区:{
标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType:{
标识:948,
标签:意外住宅火灾
altLabel:意外住宅火灾,
isSummary:虚假
},
时期:{
标识:fq_Q3_2013_14
标签:2013/14 Q3,
altLabel:2013/14 Q3,
isSummary:虚假
},
值类型:{
标识:原始,
标签:原始值,
isSummary:虚假
}
},
{
区:{
标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType:{
标识:948,
标签:意外蜗居火灾,
altLabel:意外住宅火灾,
isSummary:虚假
},
时期:{
标识: fq_Q4_2013_14,
标签:2013/14 Q4,
altLabel:2013/14 Q4,
isSummary:虚假
},
值类型:{
标识:原始,
标签:原始值,
isSummary:虚假
}
}
],
行:[
{
价值观:
{
源:515.0,
值:515.0,
格式化:515,
格式:#,## 0,
publicationStatus:已发布
},
{
源:264.0,
值:264.0,
格式化:264,
格式:#,## 0,
publicationStatus:已发布
},
{
源:254.0,
值:254.0,
格式化:254,
格式:#,## 0,
publicationStatus:已发布
},
{
源:455.0,
值:455.0,
格式化:455,
格式:#,## 0,
publicationStatus :已发布
}
]
}
]
}
我已经用 http://json2csharp.com/ 类和尝试了这种方法如: -
RootObject RO = JsonConvert.DeserializeObject< RootObject>(json_data);
和
值[] VO = JsonConvert.DeserializeObject<值[]>(json_data);
和
动态结果= JsonConvert.DeserializeObject(json_data);
也
的JavaScriptSerializer JSS =新的JavaScriptSerializer();
值[] = thisval&jss.Deserialize LT;值[]>(json_data);
等等。
什么是提取他的信息到阶级的正确途径于是我可以对他们的工作。一旦调用反序列化数据的例子将是有益的。
的主要类我是
公共类值
{
公共双源{得到;组; }
公共double值{搞定;组; }
格式的公共字符串{搞定;组; }
公共字符串格式{搞定;组; }
公共字符串publicationStatus {搞定;组; }
}
公共类行
{
公开名单< VALUE>值{搞定;组; }
}
公共类RootObject
{
公开元数据元数据{搞定;组; }
公开名单<列>列{搞定;组; }
公开名单<行>行{搞定;组; }
}
下面是一个工作的dotNet小提琴反序列化值
列表。 https://dotnetfiddle.net/7P2em6
等待几秒钟,当你加载小提琴,并注意在控制台窗口的输出。代码应该是不言自明的,但让我知道如果你需要帮助。
我也粘贴在它下面的完整性,以防dotNetFiddle不可用。
ALT =反序列化复杂JSON来的C#类>
我用 HTTP ://json2csharp.com/ 来从JSON字符串中的类
我觉得你的问题可能已在该值
是列表
的行
的对象,而这又是在列表
中的 RootObject
。换句话说,值
s的存储为列表
在列表$ C 。$ C>
完整代码
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
:使用System.IO;使用System.Net
;
使用System.Runtime.Serialization;
使用的System.Web;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json;
// SO问题:http://stackoverflow.com/questions/27132887/
//这(我)答:
//作者:湿婆Manjunath
// SO简介:http://stackoverflow.com/users/325521/shiva
公共类节目
{
公共静态无效的主要()
{
串jsonString = @{
,元数据:{
columnGrouping:
区域,
metricType ,
期,
,值类型,
],
rowGrouping:[]
},
列:[
{
区域:{
,标识:E31000040,
标签: GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType :{
,标识:948,
标签:意外蜗居火,
altLabel:意外蜗居火灾,
isSummary:虚假
},
期:{
,标识:fq_Q1_2013_14,
标签:2013/14 Q1,
altLabel:2013/14 Q1,
isSummary:虚假
},
,值类型:{
,标识:原始,
标签:原始值,
isSummary:虚假
}
},
{
区域:{
,标识:E31000040 ,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
}
metricType:{
,标识:948,
标签:意外蜗居火,
altLabel:意外蜗居火,
isSummary:虚假
},
期:{
标识符:fq_Q2_2013_14,
标签:2013/14 Q2,
altLabel:2013/14 Q2,
isSummary:虚假
},
,值类型:{
,标识:生,
,标签:原始值,
isSummary:虚假
}
},
{
区域:{
,标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType:{
,标识:948,
标签:意外蜗居火,
altLabel:意外蜗居火,
isSummary:虚假
},
期:{
,标识:fq_Q3_2013_14,
标签:2013/14 Q3,
altLabel :2013/14 Q3,
isSummary:虚假
},
,值类型:{
,标识: 原始,
标签:原始值,
isSummary:虚假
}
},
{
区域:{
,标识:E31000040,
标签:GTR曼彻斯特火,
altLabel:GTR曼彻斯特火,
isSummary:虚假
},
metricType:{
,标识, 948,
标签:意外蜗居火,
altLabel:意外蜗居火,
isSummary :虚假
},
期:{
,标识:fq_Q4_2013_14,
标签:2013 / 14 Q4,
altLabel:2013/14 Q4,
isSummary:虚假
},
,值类型 :{
,标识:原始,
标签:原始值,
isSummary:虚假
}
}
],
行:
{
,值:[
{
源:515.0,
的价值:515.0,
格式化:515,
格式化:#,# #0,
publicationStatus:发表
},
{
源:264.0,
,值:264.0,
格式化:264,
格式化:#,## 0,
publicationStatus :发表
},
{
源:254.0,
的价值:254.0,
格式化 :254,
格式化:#,## 0,
publicationStatus:发表
},
{
源:455.0,
的价值:455.0,
格式化:455,
,格式:#,## 0,
publicationStatus:发表
}
]
}
]
};
Console.WriteLine(开始JSON Deserialization\\\
);
VAR rootObject = JsonConvert.DeserializeObject< RootObject>(jsonString);
无功行= rootObject.rows;
INT rowCounter = 1;
的foreach(行oneRow行)
{
Console.WriteLine(行:+ rowCounter);
INT valueCounter = 1;
的foreach(价值oneValue在oneRow.values)
{
Console.WriteLine(值:+ valueCounter);
Console.WriteLine(来源:+ oneValue.source);
Console.WriteLine(值:+ oneValue.value);
Console.WriteLine(格式化:+ oneValue.formatted);
Console.WriteLine(publicationStatus:+ oneValue.publicationStatus);
valueCounter ++;
}
rowCounter ++;
}
Console.WriteLine(\\\
End JSON反序列化);
}
}
公共类的元数据
{
公开名单<串GT; columnGrouping {搞定;组; }
}
公共类区域
{
公共字符串标识{搞定;组; }
公共字符串标签{搞定;组; }
公共字符串altLabel {搞定;组; }
公共BOOL isSummary {搞定;组; }
}
公共类MetricType
{
公共字符串标识{搞定;组; }
公共字符串标签{搞定;组; }
公共字符串altLabel {搞定;组; }
公共BOOL isSummary {搞定;组; }
}
公共类周期
{
公共字符串标识{搞定;组; }
公共字符串标签{搞定;组; }
公共字符串altLabel {搞定;组; }
公共BOOL isSummary {搞定;组; }
}
公共类的ValueType
{
公共字符串标识{搞定;组; }
公共字符串标签{搞定;组; }
公共BOOL isSummary {搞定;组; }
}
公共类列
{
公共区域面积{搞定;组; }
公共MetricType metricType {搞定;组; }
公众工期内{搞定;组; }
公共值类型VALUETYPE {搞定;组; }
}
公共类值
{
公共双源{搞定;组; }
公共double值{搞定;组; }
格式的公共字符串{搞定;组; }
公共字符串格式{搞定;组; }
公共字符串publicationStatus {搞定;组; }
}
公共类行
{
公开名单< VALUE>值{搞定;组; }
}
公共类RootObject
{
公开元数据元数据{搞定;组; }
公开名单<列>列{搞定;组; }
公开名单<行>行{搞定;组; }
}
注意:作为列
对象,你并不需要的字段单独的类(json2csharp.com类生成器将默认为这一点)。您可以将值存储在列
对象在词典类(如果你知道他们的名字将是唯一的)。对于的实现(不同的JSON字符串,但相同的JSON模式类型,原理)看到这个小提琴: https://dotnetfiddle.net / 7bFcNM
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 brain dead.
I have this json data (apologies for length) and am trying, as a start to get the 'value' number in the 'Values' array:-
{
"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"
}
]
}
]
}
I have created classes using http://json2csharp.com/ and have tried methods such as:-
RootObject ro = JsonConvert.DeserializeObject<RootObject>(json_data);
and
Value [] vo = JsonConvert.DeserializeObject<Value[]>(json_data);
and
dynamic result = JsonConvert.DeserializeObject(json_data);
also
JavaScriptSerializer jss = new JavaScriptSerializer();
Value [] thisval = jss.Deserialize<Value[]>(json_data);
Among others. What would be the correct way to extract his information into the classes so then i could work on them. An example of calling the data once deserialized would be helpful. The main classes I have are
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; }
}
Here's a working dotNet Fiddle that deserializes the Values
List. https://dotnetfiddle.net/7P2em6
Wait few seconds when you load the fiddle and notice the output in the Console window. The code should be self-explanatory, but let me know if you need help.
I have also pasted it below for completeness in case dotNetFiddle is not available.
Console Output:
I used http://json2csharp.com/ to generate the classes from the JSON string.
I think your issue might have been that Values
is a List
inside the Row
object, which, in turn, is a List
inside the RootObject
. In other words, Value
s are stored as a List
inside a List
.
Complete Code Listing
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; }
}
NOTE: For the columns
object, you don't need separate classes for the fields (json2csharp.com class generator will default to that). You can store the values in the columns
object in a dictionary class (if you know that their names will be unique). For an implementation of that (different JSON string, but same json schema type, principle) see this fiddle: https://dotnetfiddle.net/7bFcNM
这篇关于复杂的JSON转换为C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!