我从功能应用程序通过服务总线主题触发器收到以下响应,在这里我想过滤出记录类型为DOC TYPE AS为“ REC”且ComputerModel为“ Yuli”的记录ID。因此,对于从服务总线触发器获得的以下响应,我应该获得值ID:“ MARAS_1S4810L8010016000075_20201015T130058Z”,我将在代码中进一步查询。我想为我完成这段代码提供一些帮助。
在控制台中响应mySbMsg:
[{“ Id”:“ MARAS_1S4810L8010016000075_20201015T130058Z”,“ DocType”:“ REC”,“ Tags”:[],“ Properties”:{“ Access”:“ COMMON”,“ BIOSSerialNumber”:“ 0075”,“ CalibHash“:” aoI8zg“,” ComputerBuild“:” EE-FVT“,” ComputerModel“:” Yuii“,” Customer“:” MAARS“,” Location“:” Mumb“,” LogPresenceDuration“:” 120“,”型号”:“ YTOPI 44880132-aoI8zg”,“ CompanyXYZ”:“ false”,“ RadarFPS”:“ 4”,“ RadarModule”:“ YTOPI”,“ RadarMount”:“ Yuii”,“ RadarRevision”:“ Rev4”, “ RecorderRelease”:“ Recorder_Platform_Minimal-Release049_20200924T143831”,“ RecordingType”:“ UX”,“ ResistorValueOhm”:“ 75”,“ ScreenTiltAngle”:“ 120”,“ UniqueSystemID”:“ 1S4810L8010016000075”,“ UserChairX”:“ 0, “ UserChairZ”:“ 0”,“ UserSittingMode”:“倾斜右侧”,“ UserWalkInRepetitions”:“ 1”,“ uDriverVersion”:“ v2_0_6”},“类别”:[],“触发器”:true,“已收到“:” 2020-10-15T14:58:25.3357416Z“}, {“ Id”:“ MAARS_Yuii_UX | MAARS_2.1.1.1_1S4810L8010016000075_20201015T130732Z”,“ DocType”:“ ASS”,“标签”:[],“属性”:{“访问”:“ COMMON”,“ BIOSSerialNumber”:“ 16000075” ,“ CalibHash”:“ aoI8zg”,“ ComputerBuild”:“ EE-FVT”,“ ComputerModel”:“ Yuii”,“客户”:“ MAARS”,“位置”:“ Mumb”,“ LogPresenceDuration”:“ 120” ,“ Model”:“ YTOPI 44880132-aoI8zg”,“ CompanyXYZ”:“ false”,“ RadarFPS”:“ 4”,“ RadarModule”:“ YTOPI”,“ RadarMount”:“ Yuii”,“ RadarRevision”:“ Rev4 “,” RecorderRelease“:” Recorder_Platform_Minimal-Release049_20200924T143831“,” RecordingType“:” UX“,” ResistorValueOhm“:” 75“,” ScreenTiltAngle“:” 120“,” UniqueSystemID“:” 1S4810L8010016000075“,” UserChairX“:” 0 “,” UserChairZ“:” 0“,” UserSittingMode“:”向后倾斜“,” UserWalkInRepetitions“:” 1“,” uDriverVersion“:” v2_0_6“},”类别“:[],”触发器“:true,”收到“:”“ 2020-10-15T14:58:25.3357755Z”}]
代码:
namespace ServiceBusCopyFunction
{
public static class GetMetadataFromSB
{
[FunctionName("GetMetadataFromSB")]
public static void Run([ServiceBusTrigger("TopicName", "Topic Subscription", Connection = "AzureServiceBusString")] string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
}
}
答案 0 :(得分:2)
我弄清楚了,学习了如何创建Model和反序列化JSON数据。这是对“ DocType”属性进行过滤的代码:
var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
List<MyItem> lists = JsonConvert.DeserializeObject<List<MyItem>>(mySbMsg, jsonSerializerSettings);
List<string> filteredResult = (from s in lists
where s.DocType == "REC"
select s.Id).ToList();
}
catch(Exception e )
{
log.LogError( $"Error while catching messages; {e.Message}\n" );
}
}
public class MyItem
{
public string Id;
public string DocType;
public string Project;
public string ProjectId;
public MyProperties Properties;
}
public class MyProperties
{
public string Access;
public string BIOSSerialNumber;
}
}
}