json反序列化的动态数据模型 [英] Dynamic data modal for json deserialization

查看:86
本文介绍了json反序列化的动态数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为单个API获得2种不同的数据模型,例如



作为第一个记录

i am getting 2 different data models for single API like

for first record

public class GetPartyRs
    {
        public CustStatus CustStatus { get; set; }
        public IPBaseData IPBaseData { get; set; }
        public IPDemoDataMod IPDemoDataMod { get; set; }
        public AltIdData AltIdData { get; set; }
        public XrefPartyId xrefPartyId { get; set; }
        public List<SupplementalInfo> SupplementalInfo { get; set; }
        public CitizenshipInfo CitizenshipInfo { get; set; }
    }



为第二记录


for second record

public class GetPartyRsCustres
   {
      
       public CustStatusCustres CustStatus { get; set; }
       public IPBaseDataCustres IPBaseData { get; set; }
       public IPDemoDataModCustres IPDemoDataMod { get; set; }
       public List<AltIdDataCustres> AltIdData { get; set; }
       public List<XrefPartyIdCustres> xrefPartyId { get; set; }
       public List<SupplementalInfoCustres> SupplementalInfo { get; set; }
       public CitizenshipInfoCustres CitizenshipInfo { get; set; }
   }



如何反序列化此数据模型


how to deserialize this data model

推荐答案

在反序列化数据之前,你需要序列化模型的实例。您可以使用类 System.Runtime.Serialization.Json.DataContractJsonSerializer

DataContractJsonSerializer Class(System.Runtime.Serialization.Json) [ ^ ]。



如果您的数据源始终是.NET代码,那么您无需做任何事情;一切都已经完成了。 数据契约机制是类型无关的,它允许您序列化对象图(不幸的是,对于JSON,它仅限于,但是这可能是你所需要的),然后从资源中恢复它。您只需添加一些数据协定属性( [DataContract] [DataMember] )。从你的类创建数据合同它完全不侵入,它不会影响你的代码的功能。请参阅:使用数据合同 [ ^ ]。



如果您必须在其他地方生成JSON并反序列化它(通常在JavaScript中完成),您必须将JavaScript与模型匹配,这应该非常简单。这就是为什么.NET中的序列化非常有用的原因:您可以创建一些JSON数据样本,这些样本可以在您设计JavaScript代码时为您提供很多帮助。



And最后,在最坏的情况下,当已经给出JSON数据并且您无法更改它时,您将不得不以不同的方式修改.NET数据模型和/或解析JSON。此外,您可以将其解析为不同的模型,您可以稍后在其上进行映射。从您的问题来看,我认为您不需要它,但如果您需要,我也会解释这一部分。



-SA
Before deserializing the data, you need to serialize the instance of the model. You can use the class System.Runtime.Serialization.Json.DataContractJsonSerializer:
DataContractJsonSerializer Class (System.Runtime.Serialization.Json)[^].

If your source of data is always .NET code, there is nothing you have to do; everything is already done for your. The Data Contract mechanism is type-agnostic, it allows you to serialize an object graph (unfortunately, for JSON it's limited to a tree, but this is probably all you need) and later restore it from resource. All you need is adding some data contract attributes ([DataContract] and [DataMember]). Creation of data contract out of you classes it totally non-intrusive, it cannot affect the functionality of your code. Please see: Using Data Contracts[^].

If you have to generate JSON somewhere else and deserialize it, which is usually done in JavaScript, you have to match your JavaScript to your model, which should be quite easy. And that's why serialization in .NET would be very useful: you can create some samples of JSON data which can help you a lot when you design your JavaScript code.

And, finally, in worst case when the JSON data is already given and you cannot change it, you would have to modify your .NET data model and/or parse JSON in a different way. Also, you can parse it to a different model which you can later map on yours. From your question, I don't think you need it, but if you need, I'll explain this part, too.

—SA


如果您使用System.IO(用于文件处理)和System.Runtime.Serialization(WCF)库来序列化和反序列化,那么在以下情况下您将没有问题:



1.你没有任何字段,类等声明'静态'或'只读'



1.a.我已经读过JSON序列化程序将序列化静态字段,但是我没有为自己验证这个。



2.你用[DataContract]属性装饰每个Class。请注意,每个班级是指从您希望读/写的任何班级继承的所有班级。



2.a.如果给定的类继承了类似于通用列表的东西,它实现了ISerializable,那么你不需要也不能使用[DataContract]



3.你装饰每一个具有[DataMember]属性的字段,属性或数据结构



4.在某些情况下,您可能必须在Class标题中添加[KnownType]属性协助WCF设施



草图:
If you use the System.IO (for file handling) and System.Runtime.Serialization (WCF) libraries to serialize and de-serialize, you'll have no problem if:

1. you do not have any fields, classes, etc. declared 'static, or 'readonly

1.a. I have read that the JSON serializer will serialize static fields, but I have not verified this for myself.

2. you adorn every Class with the [DataContract] attribute. note that by "every class" I mean all classes that inherit from any class you wish to read/write.

2.a. if a given class inherits from something like a Generic List, which implements ISerializable, you do not need to, and cannot, use [DataContract]

3. you adorn every field, property, or data-structure with the [DataMember] attribute

4. you may, in certain cases have to add the [KnownType] attribute in the Class heading to assist the WCF facility

Sketch:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace Whatever
{
    [DataContract]
    public class WhateverSaveRestore
    {
        [DataMember]
        public List<GetPartyRs> ListOParties { set; get; }

        [DataMember]
        public List<GetPartyRsCustres> ListOCustres { set; get; }

        public static string FilePath = "some valid filepath";

        public static DataContractSerializer serializer = new DataContractSerializer(typeof(WhateverSaveRestore));

        public static void Serialize(WhateverSaveRestore wsr)
        {
            // WCF serialize cod
        }

        public static WhateverSaveRestore DeSerialize(string filePath)
        {
            WhateverSaveRestore wsr;

            // WCF de-serialize code

            return wsr;
        }
    }

    [DataContract]
    public class GetPartyRs
    {
        // adorn every field, property with [DataMember]
    }

    [DataContract]
    public class CustStatus
    {
        // adorn every field, property with [DataMember]
    }

    [DataContract]
    public class GetPartyRsCustres
    {
        // adorn every field, property with [DataMember]
    }

    [DataContract]
    public class CustStatusCustres
    {
        // adorn every field, property with [DataMember]
    }
}


这篇关于json反序列化的动态数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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