将 JSON 传递给 Dynamics CRM SharedVariable 而不是字符串
Pass JSON to Dynamics CRM SharedVariable instead of string
我有序列化为 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()
第二次尝试是 return CRM 实体列表,希望 Dynamics 将其序列化。
最后一次尝试是将 JSON 字符串转换为一个对象:
_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), (Object)_loggerContainerUser.ConvertToJson()
没有任何效果,我总是得到双引号中的 JSON 字符串。
有人有什么建议吗?
您必须“创建一个 DataContract class 来为 JSON 数据定义数据模型”并使用 DataContractJsonSerializer
将 JSON 字符串反序列化(逆向工程)为对象在另一端。 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();
}
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);
}
我有序列化为 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()
第二次尝试是 return CRM 实体列表,希望 Dynamics 将其序列化。
最后一次尝试是将 JSON 字符串转换为一个对象:
_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), (Object)_loggerContainerUser.ConvertToJson()
没有任何效果,我总是得到双引号中的 JSON 字符串。
有人有什么建议吗?
您必须“创建一个 DataContract class 来为 JSON 数据定义数据模型”并使用 DataContractJsonSerializer
将 JSON 字符串反序列化(逆向工程)为对象在另一端。 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();
}
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);
}