繁体   English   中英

如何在强类型 class 中转换从 LUISRuntimeClient 返回的 Prediction.Entities

[英]How to convert Prediction.Entities returned from LUISRuntimeClient in a strongly typed class

我正在 VS2019 中使用 Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime package 测试 Luis 应用程序。 我从预测中得到了一个实体列表,但它是一个具有许多属性的 json。 我想以优雅的方式对其进行编码,而不处理 json。 是否有一些 class 可以将实体转换为它并使用?

不,没有 class 可供我们将 json 实体转换为它,我们需要自己解析 json。 We can create a class which its attributes match the entity json properties, and then convert the json to the class object.

将 json 解析为 class 并不难,下面是一个示例供您参考:

public class User
{
    public User(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jUser = jObject["user"];
        name = (string) jUser["name"];
        teamname = (string) jUser["teamname"];
        email = (string) jUser["email"];
        players = jUser["players"].ToArray();
    }

    public string name { get; set; }
    public string teamname { get; set; }
    public string email { get; set; }
    public Array players { get; set; }
}

// Use
private void Run()
{
    string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
    User user = new User(json);

    Console.WriteLine("Name : " + user.name);
    Console.WriteLine("Teamname : " + user.teamname);
    Console.WriteLine("Email : " + user.email);
    Console.WriteLine("Players:");

    foreach (var player in user.players)
        Console.WriteLine(player);
 }

还有许多其他解决方案/示例可以转换 json。

是否有一些 class 可以将实体转换为它并使用?

Andrew, I have successfully deserialized a LUIS v2 REST endpoint JSON response into Microsoft.Bot.Builder.Luis.Models.LuisResult from the Microsoft.Bot.Builder v3 package (note could not find a compatible type in the latest v4 package), which比使用原始 JSON 更优雅。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM