0

I am trying to pull data and display it from a rest API using C#. However I am having trouble looping through it and displaying each individual item I have tried code like so

private void ParseAndDisplay(JsonValue json)
{
    JsonValue teamData = json["teams"];
    TextView name = FindViewById<TextView>(Resource.Id.txtName);

    foreach(var team in teamData)
    {
        name.Text = team["name"];
    }
}

What is it I do differently? For now I am simply trying to loop through and display all the teams "name" in one text view.

Christos
  • 53,228
  • 8
  • 76
  • 108
Cathy Regan
  • 251
  • 1
  • 2
  • 7

1 Answers1

0

Ok. lets assume this is your json object:

{"Tables":[
  {"field1":1,"field2":2}
  ,{"field1":1,"field2":2
  }]}

at first we create a object like this:

public class Table
{
    public int field1 { get; set; }
    public int field2 { get; set; }
}

public class RootObject
{
    public List<Table> Tables { get; set; }
}

then just deserilize your json into object then iterate it like this:

    var objs = JsonConvert.DeserializeObject<RootObject>(yourJson);
    foreach(var t in objs.Tables)
    {
        //do something
    }
Mohammad
  • 2,724
  • 6
  • 29
  • 55