Error trying to deserialize JSON from a webpage

1

I'm using Luis.ai to create my neural network of intents and the Microsoft Bot Framework to create my chatbot, but I can not read a json that Luis is generating for me.

Luis's class:

public static async Task<LuisResult> GetResponse(string message)
{
    using (var client = new HttpClient())
    {
        var url = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/06145033-fb92-485e-acd5-0bf432e892d5?subscription-key=a66048dcba8e4dcd845c91ebfff5a031&verbose=true&timezoneOffset=-180&q=" + message;

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(url);

        if (!response.IsSuccessStatusCode) return null;
        var result = await response.Content.ReadAsStreamAsync();

        var js = new DataContractJsonSerializer(typeof(LuisResult));

        return (LuisResult)js.ReadObject(result);
    }
}

As I'm calling you:

Console.WriteLine(luis.GetResponse(activity.Text.ToLower()).Result.Intents[0].Intent.ToString());

I'm doing everything locally.

EDIT:

When I debug, it stays infinitely in this line:

var response = await client.GetAsync(url);

I created another solution, I did the whole process of downloading Json, and in that new solution it worked perfectly ...

    
asked by anonymous 27.08.2017 / 04:04

2 answers

4

Important You need to get the NewtonSoft.Json package from Nuget . / p>

You change these lines of code

var js = new DataContractJsonSerializer(typeof(LuisResult));

return (LuisResult)js.ReadObject(result);

for

var js=Newtonsoft.Json.JsonConvert.DeserializeObject<LuisResult>(result);

Your code looks like this:

public static async Task<LuisResult> GetResponse(string message)
{
    using (var client = new HttpClient())
    {
        var url = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/06145033-fb92-485e-acd5-0bf432e892d5?subscription-key=a66048dcba8e4dcd845c91ebfff5a031&verbose=true&timezoneOffset=-180&q=" + message;

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(url);

        if (!response.IsSuccessStatusCode) return null;
        var result = await response.Content.ReadAsStringAsync();

        return Newtonsoft.Json.JsonConvert.DeserializeObject<LuisResult>(result);

    }
}
    
27.08.2017 / 04:16
1

I do not know exactly what the problem is, but I was able to fix using the following line of code:

HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(continueOnCapturedContext: false);

Maybe later I'll look into this more and come back and tell you why the error was occurring.

    
27.08.2017 / 14:40