Forums - C# Corner

Forum guidelines

how can i get the json intent data in to c#?

Feb 28 2018 11:13 PM
Below is my class for the json output:
  1. class PwdResetRequest  
  2. {  
  3. public class TopScoringIntent  
  4. {  
  5. public string intent { getset; }  
  6. public double score { getset; }  
  7. }  
  8. public class Intent  
  9. {  
  10. public string intent { getset; }  
  11. public double score { getset; }  
  12. }  
  13. public class Resolution  
  14. {  
  15. public string value { getset; }  
  16. }  
  17. public class Entity  
  18. {  
  19. public string entity { getset; }  
  20. public string type { getset; }  
  21. public int startIndex { getset; }  
  22. public int endIndex { getset; }  
  23. public Resolution resolution { getset; }  
  24. }  
  25. public class RootObject  
  26. {  
  27. public string query { getset; }  
  28. public TopScoringIntent topScoringIntent { getset; }  
  29. public List<Intent> intents { getset; }  
  30. public List<Entity> entities { getset; }  
  31. }  
  32. }  
Luis Return result:
  1. "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 } ] }  
I have developed the below code for getting the data from the json.
  1. var uri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;  
  2. var response = await client.GetAsync(uri);  
  3. var strResponseContent = await response.Content.ReadAsStringAsync();  
  4. var json = await response.Content.ReadAsStringAsync();  
  5. var token = JObject.Parse(json).SelectToken("entities");  
  6. foreach (var item in token)  
  7. {  
  8. var request = item.ToObject<Entity>();  
  9. }  
  10. // Display the JSON result from LUIS  
  11. Console.WriteLine(strResponseContent.ToString());  
  12. }  
And I only want the data from the "TopScoringIntent". How can I get that using C#? Below is the code that I tried but nothing came out: Message=Error reading JObject from JsonReader. Path '', line 0, position 0. Source=Newtonsoft.Json

Answers (7)