下面是我的 json 输出类:
class PwdResetRequest
{
public class TopScoringIntent
{
public string intent { get; set; }
public double score { get; set; }
}
public class Intent
{
public string intent { get; set; }
public double score { get; set; }
}
public class Resolution
{
public string value { get; set; }
}
public class Entity
{
public string entity { get; set; }
public string type { get; set; }
public int startIndex { get; set; }
public int endIndex { get; set; }
public Resolution resolution { get; set; }
}
public class RootObject
{
public string query { get; set; }
public TopScoringIntent topScoringIntent { get; set; }
public List<Intent> intents { get; set; }
public List<Entity> entities { get; set; }
}
}
Luis 返回结果:
{
"query": "create a new password for sjao9841@demo.com",
"topScoringIntent": {
"intent": "ResetLANIDpassword",
"score": 0.9956063
},
"intents": [
{
"intent": "ResetLANIDpassword",
"score": 0.9956063
},
{
"intent": "None",
"score": 0.179328963
}
],
"entities": [
{
"entity": "sjao9841@demo.com",
"type": "builtin.email",
"startIndex": 26,
"endIndex": 47
}
]
}
我开发了以下代码来从 json 中获取数据。
var uri =
"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" +
luisAppId + "?" + queryString;
var response = await client.GetAsync(uri);
var strResponseContent = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync();
var token = JObject.Parse(json).SelectToken("entities");
foreach (var item in token)
{
var request = item.ToObject<Entity>();
}
// Display the JSON result from LUIS
Console.WriteLine(strResponseContent.ToString());
}
我只想要来自“TopScoringIntent”的数据。我如何使用 C# 获得它?下面是我试过但没有结果的代码:Message=从 JsonReader 读取 JObject 时出错。路径 '',第 0 行,位置 0。Source=Newtonsoft.Json
我可能没有理解您的意图,但如果您只关心特定值而不是整个 javascript 对象,则可以执行以下操作。
dynamic json = JsonConvert.Deserialize(data);
var score = json.TopScoringIntent.Score;
在 TopScoringIntent
中提供分数的具体值。显然,您也可以通过稍微修改来很好地构建一个集合。
foreach(var point in json.Intents)
Console.WriteLine($"{point[1]} or {point.score}");
我相信这就是您正在寻找的,从您的对象中获得特定的值(value)。请注意动态很有用,但这种方法相当快速且肮脏,可能不适合您的实现。
我是一名优秀的程序员,十分优秀!