简体   繁体   English

在C#类中转换JSon对象

[英]Convert JSon object in c# classes

I have large set of Json data returned from service. 我从服务返回了大量Json数据。 Now I want to convert this data to equivalent c# classes. 现在,我想将此数据转换为等效的c#类。 Some Json data is related to metadata and some related to actual dataset. 一些Json数据与元数据有关,而某些与实际数据集有关。 Json data is in flat hierarchy. Json数据处于统一层次结构中。 I have tried JSON.Net as well but it converts the properties with same name. 我也尝试过JSON.Net,但它会转换具有相同名称的属性。 How i can convert the Json data in c# concrete classes ? 我如何在c#具体类中转换Json数据? I have draw a rough picture but just for the basic understanding (I am not much good in drawing :) ) 我已经画了一个粗略的图画,但是只是为了基本的理解(我在绘画中不是很好:)) 在此处输入图片说明

You say your JSON is actually a flat structure. 您说您的JSON实际上是一个平面结构。 Obviously JSON.NET has no notion of which properties belong to the actual days and which are metadata. 显然,JSON.NET不知道哪个属性属于实际日期,哪些是元数据。

So let JSON.NET deserialize into the flat classes, and then map those classes into the nested classes that you want. 因此,让JSON.NET反序列化为平面类,然后将这些类映射到所需的嵌套类中。

You can use AutoMapper for this. 您可以为此使用AutoMapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM