Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的JSON消息处理程序#_C#_Json_Mqtt - Fatal编程技术网

C# C语言中的JSON消息处理程序#

C# C语言中的JSON消息处理程序#,c#,json,mqtt,C#,Json,Mqtt,我试图通过MQTT为接收到的JSON字符串创建一个消息处理程序。我有一个工作类型的信息 这是我的密码: 静态无效客户端\u MQTTMSGPPublishReceived(对象发送方,MQTTMSGPPublishEventArgs e) { //处理收到的消息 Console.WriteLine(“Received=“+Encoding.UTF8.GetString(e.Message)+”on topic“+e.topic”); 字符串源=Encoding.UTF8.GetString(e.

我试图通过MQTT为接收到的JSON字符串创建一个消息处理程序。我有一个工作类型的信息

这是我的密码:

静态无效客户端\u MQTTMSGPPublishReceived(对象发送方,MQTTMSGPPublishEventArgs e)
{
//处理收到的消息
Console.WriteLine(“Received=“+Encoding.UTF8.GetString(e.Message)+”on topic“+e.topic”);
字符串源=Encoding.UTF8.GetString(e.Message);
动态数据=JObject.Parse(源);
Console.WriteLine(data.content.value);
//此data.content.value对消息1有效,但对消息2崩溃
}
如果输入了2条(或更多)不同的MQTT消息,如何避免在消息处理程序中使用大量try捕获

以下是我目前的两类消息:

/// message 1:
{
    "header": {
        "headeritem": "exampleheader",
        "type": "headertype",
        "info": "headerinfo"
    },
    "content": {
        "name": "contentname",
        "value": true
    }
}

/// message 2:
{
    "header": {
        "headeritem": "exampleheader",
        "type": "headertype",
        "info": "headerinfo"
    },
    "content": {
        "item1": "exampleitem1",
        "item2": "exampleitem2",
        "item3": [
          {
            "item3type1": "exampletype1.1",
            "item3type2": "exampletype2.1",
            "item3type3": "exampletype3.1",
            "item3type4": ["0xFFAA"]
          },
          {
            "item3type1": "exampletype1.2",
            "item3type2": "exampletype2.2",
            "item3type3": "exampletype3.2",
            "item3type4": ["0xFFBB"]
          },
          {
            "item3type1": "exampletype1.3",
            "item3type2": "exampletype2.3",
            "item3type3": "exampletype3.3",
            "item3type4": ["0xFFCC"]
          }
        ]
    }
}

当您不知道将出现什么JSON类型时,可以使用对动态数据类型的反射来检查该属性是否存在

就像这里解释的:

然而,当不同的JSON格式仅限于少数时,您最好创建硬类型类来解析JSON

就像这里解释的:


这样,您就不必担心属性是否存在。

Thx,现在使用硬类型类似乎很有魅力。起初我有点挣扎,因为我为我的两条消息创建了两个类,但现在我将它们组合在一个类中,这非常好用!我曾经创建过这些类。