Error while trying to deserialize JSON from a web page

Asked

Viewed 84 times

1

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

Luis' 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 do the debugging, it gets infinitely on this line:

var response = await client.GetAsync(url);

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

  • You can switch to the Newtonsoft.Json Library?

  • I believe so, but what would the code look like? @Tiago

  • Minimal changes

  • So far you have no error, are 2 warnings. The first, about the debug of System.Net.Http.WebRequest.dll and the debug option "just my code". The second Warning speaks of the passing of the JWT token, however the message is just a Warning that is not passing the token.

2 answers

4

Important You need to get the Newtonsoft.Json package on Nuget.

You exchange 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 would look 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);

    }
}
  • I edited the question, take a look.

1


I don’t know exactly what the problem is, but I was able to fix it using the following line of code:

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

Maybe later I’ll research more about it and come back to talk about why the error was occurring.

  • I don’t know why either, but I’ve been there and solved it the same way. Maybe I could turn into a question, what do you think?

  • @Tiagos I think coming. Create there I leave my +1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.