【问题标题】:Deserialize Json for CRM Entities为 CRM 实体反序列化 Json
【发布时间】:2018-03-01 16:25:05
【问题描述】:

我有以下 Json 定义了必须存储在自定义 CRM 实体中的特定配置:

{
"useraccountid": "U12345",
"profiles": [
    {
        "applicationrole": "RelationshipManager",
        "maindatascopetypecd": 858000001,
        "organisationalunitno": "10000000",
        "ishierachical": 1
    },
    {
        "applicationrole": "CountrySpecialist",
        "maindatascopetypecd": 858000002,
        "attributetypecd": 858000000,
        "attributevalue": "SY",
        "isreadonly": 0
    }
]
}

每个用户可以有多个用户配置文件。这些数据最终需要写入自定义 CRM 实体。 “useraccountid”是对系统用户(实体引用)的查找。

我已经拥有的是这样的反序列化器:

public static T JsonDeserialize<T>(string jsonString)
{
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(ms);
        return obj;
}

父类的类是这样的:

[DataContract]
class CrmUserProvisioning
{

    [DataMember]
    public String clm_useraccountid
    {
        get; set;
    }

    // will be set on runtime
    public DateTime clm_createdon
    {
        get; set;
    }

    [DataMember]
    public List<CrmProfile> clm_profiles { get; set; } = new List<CrmProfile>();

}

对于个人资料

[DataContract]
public class CrmProfile
{

    [DataMember]
    public Guid clm_userprovisioningid
    {
        get; set;
    }

    [DataMember]
    public string clm_applicationrole
    {
        get; set;
    }

    [DataMember]
    public int clm_maindatascopetypecd
    {
        get; set;
    }

    [DataMember]
    public string clm_organisationalunitno
    {
        get; set;
    }

    [DataMember]
    public bool clm_ishierachical
    {
        get; set;
    }

    [DataMember]
    public int clm_applyingtypecd
    {
        get; set;
    }

    [DataMember]
    public int clm_globalopenaccesscd
    {
        get; set;
    }

    [DataMember]
    public int clm_attributetypecd
    {
        get; set;
    }

    [DataMember]
    public string clm_attributevalue
    {
        get; set;
    }

    [DataMember]
    public bool clm_isreadonly
    {
        get; set;
    }

}

配置文件配置中缺少的字段不会被反序列化。我对如何反序列化此配置感到困惑,因为配置可以包含许多配置文件类但只有一个父类(UserProvisioning)。

有人能把我引向正确的方向吗?非常感谢任何帮助。

亲切的问候

更新 02.03.2018

将 Jsion 反序列化为对象后,我现在需要将对象存储到相应的 Microsoft Dynamics CRM 实体中。实体和属性的命名类似于对象名和属性名。

我已经有了组织服务等。我只需要知道如何将对象映射到常规的 crm 创建或更新请求。

如果有人能帮我解决这个问题,将不胜感激。

【问题讨论】:

    标签: c# json dynamics-crm deserialization


    【解决方案1】:

    使用DataContractDataMember

    [DataContract]
    class UserProvisioning
    {
        [DataMember]
        public String useraccountid
        {
            get { return this.useraccountid; }
            set { this.useraccountid = value; }
        }
    
        // Will be set on runtime
        public DateTime createdon
        {
            get { return this.createdon; }
            set { this.createdon = value; }
        }
    
        // Must declare this for the child list of Profile
        [DataMember]
        public List<Profile> profiles {get;set;}=new List<Profile>();
    }
    

    CrmProfile 中还为类设置DataContract,为属性设置DataMember

    查看文档: DataContract DataMember。只有带有DataMember 的属性才会序列化。 您还可以设置所需的属性或设置不序列化默认值。

    【讨论】:

    • 添加了有关如何通过创建或更新请求将对象存储在 CRM 实体中的附加问题。还更新了对应FRL答案的代码
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2014-05-27
    • 2016-12-21
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多