如何从LUIS只获取“intent”字段使用C#

时间:2017-10-15 10:19:24

标签: c# json luis

如何只从JIS格式代码中获取来自LUIS的C#返回结果中的“intent”字段? 我已经完成了这样的反序列化:

public class ResponseContent
{
    [BsonId]
    public string query { get; set; }
    public string topScoringIntent { get; set; }
    public string intent { get; set; }
    public string entities { get; set; }
}

public class topScoringIntent
{
    public string intent { get; set; }
    public string score { get; set; }
}

这是LUIS的返回结果:

"query": "Hello.",
"topScoringIntent": 
{
"intent": "Greeting",
"score": 0.9447609
},
"intents": 
[
{
  "intent": "Greeting",
  "score": 0.9447609
},
{
  "intent": "Request",
  "score": 0.09726282
},
{
  "intent": "None",
  "score": 5.523394E-10
}
],
"entities": []

我只想要“TopScoringIntent”中的“intent”字段。我怎样才能使用C#获得它?下面是我尝试的代码,但没有出现:

var uri = "https://southeastasia.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
var response = await client.GetAsync(uri);
var responseContent = await response.Content.ReadAsStringAsync();
var responseitem = JsonConvert.SerializeObject(responseContent,Formatting.Indented);
JToken content = JToken.Parse(responseitem);
var intentOnly = (from s in content.Children()["ResponseContent"]select s).ToList();
foreach (var item in intentOnly.Children().ToList())
{
  Console.WriteLine(item.ToObject<ResponseContent>().TopScoringIntent);
}

这不是机器人,所以我没有使用任何机器人框架来做到这一点。 谢谢。

1 个答案:

答案 0 :(得分:0)

使用以下代码完成它:

var jsonresponse = JObject.Parse(responseContent);
intentonly = jsonresponse.SelectToken("intents[0].intent").ToString();