1

我有被序列化为 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 字符串。

有人有建议吗?

4

1 回答 1

1

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

来源:序列化 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();
                }

Destination : 将 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);
                }
于 2020-03-29T14:41:11.757 回答