How to make a deserialize in a Json with C-Sharp

Asked

Viewed 132 times

0

I have the following code

private void WbRanking_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            RunOnUiThread(() =>
            {
                string json = Encoding.UTF8.GetString(e.Result);
                //lstRanking = JsonConvert.DeserializeObject<List<string>>(json);
                var obj = JsonValue.Parse(json);
                //var obj = JsonConvert.DeserializeObject<List<string>>(json);
                JsonTextReader reader = new JsonTextReader(new StringReader(json));

                ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, obj);

                while (reader.Read())
                {
                    if (reader.Value != null)
                    {
                        //string nome = obj[0];
                        lstRanking.Add("Nome " + obj[0] + "Sobrenome " + obj[1]);
                    }

                    lsvRanking.Adapter = adapter;
                }
            });
        }

How is it possible to see in the image the var obj is successfully receiving Json from a Rest api that I have local. But it’s time to put the information in the listview the system returns me this error as per the image.

Thank you in advance!

inserir a descrição da imagem aqui

1 answer

1

Normally to serialize and deserialize things I use the Newtonsoft json, in the comparisons is faster than the c# and is very simple to use.

It has in Nuget, that makes it easy enough to get the package :D

https://www.newtonsoft.com/json

I don’t know the methods you’re calling me but I usually use them like this

using Newtonsoft.Json;
...

public class Person
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
}

void PersonToJsonToPersonExample ()
{
    var person = new Person { Name = "Bob", Birthday = new DateTime (1987, 2, 2) };
    var json = JsonConvert.SerializeObject (person);
    Console.WriteLine ("JSON representation of person: {0}", json);
    var person2 = JsonConvert.DeserializeObject<Person> (json);
    Console.WriteLine ("{0} - {1}", person2.Name, person2.Birthday);
}
  • Actually I’m already using but it’s not working.

  • did not know how to put here updated the answer... hehehe

Browser other questions tagged

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