将 Encoding.ASCII.GetString 转换为 C# 中的类模型

发布于 2025-01-11 16:23:47 字数 1202 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2025-01-18 16:23:48

您的示例消息表明它是 DataClass 类型的条目集合,转换后如下所示:

public class DataClass
{
    public int sn { get; set; }
    public string nodeId { get; set; }
    public int MemberData1 { get; set; }
    public int MemberData2 { get; set; }
    public int MemberData3 { get; set; }
    public int MemberData4 { get; set; }
    public int MemberData5 { get; set; }
    public int MemberData6 { get; set; }
    public int MemberData7 { get; set; }
    public int MemberData8 { get; set; }
    public int MemberData9 { get; set; }
}

话虽如此,您需要尝试将其反序列化为 DataClass[],其中是数组。我还建议使用 System.Text.Json 库,该库已随 .NET 一起提供:)

另外,您不需要创建 JObject。只需从字符串反序列化,如下所示:

var o = JsonSerializer.Deserialize<DataClass[]>(message);

Your example message indicated that it's collection of entries of type DataClass, which after conversion looks like this:

public class DataClass
{
    public int sn { get; set; }
    public string nodeId { get; set; }
    public int MemberData1 { get; set; }
    public int MemberData2 { get; set; }
    public int MemberData3 { get; set; }
    public int MemberData4 { get; set; }
    public int MemberData5 { get; set; }
    public int MemberData6 { get; set; }
    public int MemberData7 { get; set; }
    public int MemberData8 { get; set; }
    public int MemberData9 { get; set; }
}

Having said that, you need to try deserializa it to DataClass[], which is array. I also suggest using System.Text.Json library, which is already shipped with .NET :)

Also, you do not need to create JObject. just deserialize from string, like this:

var o = JsonSerializer.Deserialize<DataClass[]>(message);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文