如何在C#.NET中反序列化嵌套的json [英] How to deserialized nested json usin C# .NET

查看:132
本文介绍了如何在C#.NET中反序列化嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何反序列化嵌套jason?



需要将jason转换为csv。



[{参数:[{名称:卡车速度,参数_资源地址:,值:0},{ 名称:HP反馈 - Alt



In,Parameter_Source_Address:,Value:7}],Snapshot_DateTimestamp: 2016-10-11T14:34:09.000Z},{参数:



[{名称:卡车速度,Parameter_Source_Address: ,值:0},{名称:HP反馈 - Alt



In,Parameter_Source_Address:,值 :7}],Snapshot_DateTimestamp:2016-10-11T14:34:10.000Z},{参数:



[{名称:卡车速度,Parameter_Source_Address:,值:0},{名称:HP反馈 - Alt



In ,Parameter_Source_Address:,Value:7}],Snapshot_DateTimestamp:2016-10-11T14:34:11.000Z},{参数:



[{Name:Truck speed,Parameter_Source_Address:,Value:0},{Name:HP Feedback - Alt



在,Parameter_Source_Address:,Value:7}],Snapshot_DateTimestamp:2016-1 0-11T14:34:12.000Z}]





班级



公共类参数

{



public string Name {get;组; } $ / $


公共字符串Parameter_Source_Address {get;组; } $ / $


public string Value {get;组; }



}



公共类快照

{



public List< parameter>参数{get;组; } $ / $


公共字符串Snapshot_DateTimestamp {get;组; }



}



我尝试了什么:



var data2 = JsonConvert.DeserializeObject< list< snapshot>>(json);



然后将转换后的列表转换为数据表和数据表到csv。



得到输出:

参数,Snapshot_DateTimestamp

System.Collections.Generic.List `1 [Json2Csv.Parameter],2016-10-11T14:34:09.000Z

System.Collections.Generic.List`1 [Json2Csv.Parameter],2016-10-11T14:34:10.000 Z

System.Collections.Generic.List`1 [Json2Csv.Parameter],2016-10-11T14:34:11.000Z

System.Collections.Generic.List` 1 [Json2Csv.Parameter],2016-10-11T14:34:12.000Z

How to deserialized nested jason?

Need to convert jason to csv.

[{"Parameter":[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:09.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:10.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:11.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:12.000Z"}]


classes

public class Parameter
{

public string Name { get; set; }

public string Parameter_Source_Address { get; set; }

public string Value { get; set; }

}

public class Snapshot
{

public List<parameter> Parameter { get; set; }

public string Snapshot_DateTimestamp { get; set; }

}

What I have tried:

var data2 = JsonConvert.DeserializeObject<list<snapshot>>(json);

Then converted list to datatable and datatable to csv.

getting output:
Parameter,Snapshot_DateTimestamp
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:09.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:10.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:11.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:12.000Z

推荐答案

使用newtonsoft json库。它可以在Nuget中使用。很多人使用它,所以谷歌搜索特定问题将产生大量可能的解决方案。



BTW,同样的问题与非常相同的数据出现了一个星期左右。在粗略搜索之后,这似乎是先前问题的重新发布。
Use the newtonsoft json library. It's available in Nuget. Lots of people using it, so a google search for specific issues will yield a plethora of possible solutions.

BTW, this very same question WITH THE VERY SAME DATA came up a week or so ago. After a cursory search, this appears to be a repost of an earlier question.


这篇关于如何在C#.NET中反序列化嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆