How to do a deserialize on a Json with C-sharp

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

As you can see in the image, the var obj is successfully receiving the Json from a location I have. But at the time of putting the information in listview the system returns this error to me as the image.

Thank you in advance!

    
asked by anonymous 05.10.2017 / 19:11

1 answer

1

Normally to serialize and deserialize things I use Newtonsoft json, in comparisons it is faster than that of C # itself and is very simple to use.

You have in Nuget, which makes it easy to get the package: D

link

I'm not aware of the methods you're calling but I usually use them

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);
}
    
05.10.2017 / 21:29