I'm testing a Luis app using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime package in VS2019. I getting a list of entities from the prediction but it's a json with many properties. I want to code with it in elengant way, without dealing with json. Is there some class that i can convert the entity into it and work with?
Asked
Active
Viewed 331 times
2 Answers
0
No, there isn't class for us to convert the json entity into it, we need to parse the json by ourself. We can create a class which its attributes match the entity json properties, and then convert the json to the class object.
It's not difficult to parse the json to class, below is a sample for your reference:
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);
}
And there are many other solutions/samples to convert the json.

Hury Shen
- 14,948
- 1
- 9
- 18
0
Is there some class that i can convert the entity into it and work with?
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 is far more elegant than working with the raw JSON.

Craigology
- 161
- 3