【问题标题】:Pass JSON to Dynamics CRM SharedVariable instead of string将 JSON 传递给 Dynamics CRM SharedVariable 而不是字符串
【发布时间】:2020-07-09 16:38:46
【问题描述】:

我有数据被序列化为 JSON,然后必须将其添加到 SharedVariable 中的 Dynamics PluginExeutionContext。问题是 JSON 内容是作为字符串传递的,不能在接收器的另一端解析为 JSON。

现在是这样传递的:

   "SharedVariables": [
         "key": "Telemetry_Log",
         "value": "JsonContent"
       },
   ]

我需要的是没有像这样的双引号的“JsonContent”

   "SharedVariables": [
         "key": "Telemetry_Log",
         "value": JsonContent
       },
   ]

首先我将数据序列化为 JSON 并将字符串传递给上下文,如下所示:

_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), _loggerContainerUser.ConvertToJson()

第二次尝试是返回 CRM 实体列表,希望 Dynamics 将其序列化。

最后一次尝试是将 JSON 字符串转换为对象:

_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), (Object)_loggerContainerUser.ConvertToJson()

没有任何效果,我总是得到双引号中的 JSON 字符串。

有人给点建议吗?

【问题讨论】:

    标签: c# json serialization dynamics-crm shared-variable


    【解决方案1】:

    您必须“创建一个将为 JSON 数据定义数据模型的 DataContract 类”并在另一端使用 DataContractJsonSerializer 将 JSON 字符串反序列化(逆向工程)为 Object。 Read more

    来源:序列化 JSON 字符串中的数据

    using (MemoryStream SerializememoryStream = new MemoryStream())
                    {
                        //create a sample data of type Student Class add details
                        Student NewStudent = new Student();
                        NewStudent.FullName = "Sam";
                        NewStudent.JobTitle = "Developer";
                        NewStudent.Contact = "808-2125454";
                        NewStudent.Country = "India";
                        NewStudent.ZIPCode = "400005";
    
                        //initialize DataContractJsonSerializer object and pass Student class type to it
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
                        //write newly created object(NewStudent) into memory stream
                        serializer.WriteObject(SerializememoryStream, NewStudent);
    
                        //use stream reader to read serialized data from memory stream
                        StreamReader sr = new StreamReader(SerializememoryStream);
    
                        //get JSON data serialized in string format in string variable 
                        string Serializedresult = sr.ReadToEnd();
                    }
    

    目标:将JSON字符串中获取的数据反序列化为对象

    using (MemoryStream DeSerializememoryStream = new MemoryStream())
                    {
                        //Json String that we get from web api 
                        string ResponseString = "{\"FullName\":\"" + "Sam" + "\",\"JobTitle\":\"" + "Developer" + "\",\"Contact\":\"" + "808-124567" + "\",\"Country\":\"" + "India" + "\",\"ZIPCode\":\"" + "400005" + "\"}";
    
                        //initialize DataContractJsonSerializer object and pass Student class type to it
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
    
                        //user stream writer to write JSON string data to memory stream
                        StreamWriter writer = new StreamWriter(DeSerializememoryStream);
                        writer.Write(ResponseString);
                        writer.Flush();
    
                        DeSerializememoryStream.Position = 0;
                        //get the Desrialized data in object of type Student
                        Student SerializedObject = (Student)serializer.ReadObject(DeSerializememoryStream);
                    }
    

    【讨论】:

    • 感谢您的回复。那么不可能将纯 json 作为值而不是字符串表示形式,例如整数或布尔值? { "key": "IsAutoTransact", "value": true }, { "key": "Count", "value": 1000 } 没有双引号?
    • @PaulRichardson 我认为只有原始数据类型可以这样,而 JSON 不是原始的
    • 你是对的。将与目的地核对。顺便说一句,我的目的地是 Azure ApplicationInsights。上下文存储在 Azure 队列中并由 Azure 函数处理,该函数将数据放入 Application Insights。
    • @PaulRichardson 实际上共享变量是用于在插件阶段之间共享数据,例如更新前和更新后..
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2014-09-04
    • 2020-05-13
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多