c# - 将 JSON 传递给 Dynamics CRM SharedVariable 而不是字符串
问题描述
我有被序列化为 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 字符串。
有人有建议吗?
解决方案
您必须“创建一个将为 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);
}
推荐阅读
- polymer - 聚合物纸标签输入未添加到表单
- java - 如何将 index.xhtml 中 inputText 输入的数据与数据库进行比较,看它是否存在于 JSF 和 Java 类中
- javascript - 我如何使用 vue js 在容器中循环卡片
- php - PHP 在 Google 地图上显示 XML 坐标
- haskell - 表示多个失败案例但只有一个成功案例的惯用类型是什么?
- ruby-on-rails - 请求规范在关联上失败 - Rails 5 API
- ios - 从 AKKeyboardView 创建 MIDI 文件
- javascript - 谷歌图表x轴每日日期不显示堆积列
- java - 哪个班级应该负责获得 AtomicLong
- graph - 图论中的削减