2

Below is my class for the json output:

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 Return result:

   {
  "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.

    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());
}

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

D.Prasad
  • 21
  • 2
  • I think that the code is 'above' not 'below', otherwise the code is missing.... – JosephDoggie Feb 28 '18 at 16:15
  • You also dont need a seperate class for `TopScoringIntent`, its just an `Intent` – maccettura Feb 28 '18 at 16:16
  • If you only want the data from `topScoringIntent`, why do you iterate over `entities`? – crashmstr Feb 28 '18 at 16:24
  • i am trying to build an api for the password reset using luis based on the utterance. I need to store the value of the json(top scoring intent in to a c# object) – D.Prasad Feb 28 '18 at 16:46
  • As you are developing in C#, why aren't you using the LUIS Client Library package provided by Microsoft? https://www.nuget.org/packages/Microsoft.Cognitive.LUIS/ you will have all the necessary objects for your treatment instead of implementing http calls manually and generating your classes that already exist in the package ;-) – Nicolas R Mar 02 '18 at 16:20

2 Answers2

0

I may be missing your intent, but if you only care about specific value rather than the entire javascript object, you could do the following.

dynamic json = JsonConvert.Deserialize(data);
var score = json.TopScoringIntent.Score;

That provides the specific value of score within TopScoringIntent. Obviously, you could also build a collection also well, by modifying slightly.

foreach(var point in json.Intents)
     Console.WriteLine($"{point[1]} or {point.score}");

I believe that is what you're looking for, to receive a specific value from your object. Please note dynamics are useful, but this approach is a fairly quick and dirty, may not be ideal for your implementation.

Greg
  • 11,302
  • 2
  • 48
  • 79
0

quicktype generated the following C# classes and JSON.Net marshaling code for your sample data:

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var pwdResetRequest = PwdResetRequest.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;
    using System.Net;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using J = Newtonsoft.Json.JsonPropertyAttribute;

    public partial class PwdResetRequest
    {
        [J("query")]            public string Query { get; set; }          
        [J("topScoringIntent")] public Ntent TopScoringIntent { get; set; }
        [J("intents")]          public Ntent[] Intents { get; set; }       
        [J("entities")]         public Entity[] Entities { get; set; }     
    }

    public partial class Entity
    {
        [J("entity")]     public string EntityEntity { get; set; }
        [J("type")]       public string Type { get; set; }        
        [J("startIndex")] public long StartIndex { get; set; }    
        [J("endIndex")]   public long EndIndex { get; set; }      
    }

    public partial class Ntent
    {
        [J("intent")] public string Intent { get; set; }
        [J("score")]  public double Score { get; set; } 
    }

    public partial class PwdResetRequest
    {
        public static PwdResetRequest FromJson(string json) => JsonConvert.DeserializeObject<PwdResetRequest>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this PwdResetRequest self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = { 
                new IsoDateTimeConverter()
                {
                    DateTimeStyles = DateTimeStyles.AssumeUniversal,
                },
            },
        };
    }
}

Now you can use System.Linq to get the maximum Ntent by Score:

var uri = $"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/${luisAppId}?${queryString}";
var response = await client.GetAsync(uri);
var json = await response.Content.ReadAsStringAsync();
var request = PwdResetRequest.FromJson(json);

var maxIntent = request.Intents.MaxBy(intent => intent.Score);

Here's the generated code in a playground so you can change the type names and other options.

David Siegel
  • 1,604
  • 11
  • 13