我有下面的代码和下面的Json样本在requestBody中返回。我想检查我的requestBody是否包含键ComputerModel,如果包含,我想获得那个值。我该怎么做呢?
Json响应我得到快速观察:(requestBody)
{
"id": "Test",
"Type": "Test",
"ComputerModel": "Testings"
"Properties": {
"Access": "CUSTOMER"
}
}
c#代码:
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
string computermodel= req.Query["ComputerModel"]; // I don't get anything here
string responseMessage = string.IsNullOrEmpty(Computermodel)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
您的json似乎不像评论中讨论的那样正确。正确的Json格式如下:
{
"id": "Test",
"Type": "Test",
"ComputerModel": "Testings",
"Properties": {
"Access": "CUSTOMER"
}
}
使用上面的Json,您可以使用JObject
解析它。req.Query
给出查询字符串参数,但您的json被发布为post body。
var jObject = JObject.Parse(requestBody );
Console.WriteLine(jObject["ComputerModel"].ToObject<string>());