将 Encoding.ASCII.GetString 转换为 C# 中的类模型
我正在尝试将 200+ 字节大小的字有效负载(使用 M2MQTT)编码为动态变量,然后将编码字符串(动态变量)转换为 JSON 对象,以便它可以映射到其相应的类对象,以便可以将其保存在 SQL 数据库中。
//C#
private void MqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
var message = Encoding.ASCII.GetString(e.Message);
JObject? jsonMessage = JObject.Parse(message);
DataClass? classList= JsonConvert.DeserializeObject<DataClass>((string)jsonMessage);
AddMyItem(classList);
}
public void AddMyItem(DataClass? passedClassList)
{
using (var context = new DataClassDbContext())
{
context.DataClass.Add(passedClassList);
context.SaveChanges();
}
}
典型消息格式
[{"sn":1027,"nodeId":"n1","MemberData1":1,"MemberData2":2,"MemberData3":3,"MemberData4":4,"MemberData5":5,"MemberData6":6,"MemberData7":7,"MemberData8":8,"MemberData9":9}]
但我目前陷入了将消息(动态字符串变量)
转换为JSON对象
以便将其传递给< code>classList 用于类模型重新映射 - 不太确定为此实现的最佳语法是什么。
最终目标是将 message
的成员值映射到 DataClass
的 Class 对象,但当然,在尝试直接传递值时会出现隐式转换类型错误,即,vFarm vFarmCo2PdList = message;
。因此,我想知道是否需要首先以某种方式处理 message
变量。
I am trying to encode a 200+ byte-size word payload (Using M2MQTT) into a Dynamic variable
and thereafter convert the encoded string (Dynamic variable)
into a JSON object so that it can be mapped to its corresponding Class object so that it can be saved in a SQL Database.
//C#
private void MqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
var message = Encoding.ASCII.GetString(e.Message);
JObject? jsonMessage = JObject.Parse(message);
DataClass? classList= JsonConvert.DeserializeObject<DataClass>((string)jsonMessage);
AddMyItem(classList);
}
public void AddMyItem(DataClass? passedClassList)
{
using (var context = new DataClassDbContext())
{
context.DataClass.Add(passedClassList);
context.SaveChanges();
}
}
Typical Message Format
[{"sn":1027,"nodeId":"n1","MemberData1":1,"MemberData2":2,"MemberData3":3,"MemberData4":4,"MemberData5":5,"MemberData6":6,"MemberData7":7,"MemberData8":8,"MemberData9":9}]
But I am currently stuck at the conversion of message (Dynamic string variable)
into a JSON object
in order to pass it to classList
for Class Model remapping - Not too sure what's the best syntax to implement for this.
The end goal is to map message
's member value into a Class object of DataClass
but of course, there is an implicit conversion type error when trying to pass the values directly, i.e., vFarm vFarmCo2PdList = message;
. Hence, I am wondering if I need to process message
variable in some way first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例消息表明它是
DataClass
类型的条目集合,转换后如下所示:话虽如此,您需要尝试将其反序列化为
DataClass[]
,其中是数组。我还建议使用 System.Text.Json 库,该库已随 .NET 一起提供:)另外,您不需要创建 JObject。只需从字符串反序列化,如下所示:
Your example message indicated that it's collection of entries of type
DataClass
, which after conversion looks like this:Having said that, you need to try deserializa it to
DataClass[]
, which is array. I also suggest usingSystem.Text.Json
library, which is already shipped with .NET :)Also, you do not need to create JObject. just deserialize from string, like this: