Xamarin - Accessing API that returns JSON

0

Next, I'm VERY new to this API issue.
I created my first web application .net Framework 4.5.2.
I created it pretty standard even (according to VS 2015), and I created a test controller "TestController.cs", I tested it locally and it works, I uploaded it to a host, and it works too ( link ). Follow the GET method of my API

public string Get(int id)
    {
        ClassRetorno cr = new ClassRetorno();
        cr.Id = 1;
        cr.Nome = "BraKa Yedmore";
        cr.Email = "[email protected]";
        JavaScriptSerializer ser = new JavaScriptSerializer();
        string output = ser.Serialize(cr);
        return output;
    }

My intention is that when you tap the button, an Alert will be displayed with this information (read from the API return). Here is the method you performed by tapping the button:

private async void buttonConnect_Clicked(object sender, EventArgs e)
    {
        ClassRetorno cr = await GetData();
        string retorno = "Id: " + cr.Id + ";\nNome: " + cr.Nome + ";\nE-Mail: " + cr.Email;
        await DisplayAlert("Alert", retorno, "Ok");
    }

Follow the GetData method

public async Task<ClassRetorno> GetData()
    {
        using (var client = new HttpClient())
        {
            var response = await client.GetAsync("http://www.afectus.com.br/api/teste/0");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                //string contentTest = "{\"id\":1,\"nome\":\"BraKa Yedmore\",\"email\":\"[email protected]\"}";
                ClassRetorno cr = JsonConvert.DeserializeObject<ClassRetorno>(content);
                return cr;
            }
            else
            {
                return null;
            }
        }
    }

I tested the contentTest string just to see if it works. And it works!
However it does not work when content pops up with API return.
This is the content of content returned from the API

string content = "\"{\\"Id\\":1,\\"Nome\\":\\"BraKa Yedmore\\",\\"Email\\":\\"[email protected]\\"}\"";

And this is the Exception presented in the attempt to deserialize content

  

Newtonsoft.Json.JsonSerializationException: Error converting value "{" Id ": 1," Name ":" BraKa Yedmore "," Email ":" [email protected] "} to type 'MobileApp.Classback'. Path '', line 1, position 69.

And this is the class ClassRetorno.cs

public class ClassRetorno
{
    private int id;
    private string nome;
    private string email;
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
}

I know that my question has been somewhat extended, but I have tried to pass on all possible information. If someone knows and can help me or guide me how to solve this problem.

Thank you!

    
asked by anonymous 05.09.2016 / 19:28

1 answer

0

I found the problem
I was trying to use the JsonConvert.DeserializeObject (content) directly from the response I received from the API. To solve, I had to deserialize without defining a type, and then deserialize defining the type

if (response.IsSuccessStatusCode)
            {
                var retorno = await response.Content.ReadAsStringAsync();
                object content = JsonConvert.DeserializeObject(retorno);
                ClassRetorno cr = JsonConvert.DeserializeObject<ClassRetorno>(content.ToString());
                return cr;
            }

Thank you!

    
06.09.2016 / 16:00