How get the array size using dynamic type

Viewed 8024

I am getting an array when I deserialize my JSON.

I can access the array with foreach.

dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
foreach (dynamic result in  obj.Results.output1.value.Values)
{
}

But I need to get size of the array to access the last element direct.

Eg.

obj.Results.output1.value.Values[size-1]

How can I do that?

Edit 1

I need to get for example the "Y" in "Values"

{
  "Results": {
    "output1": {
      "type": "table",
      "value": {
        "ColumnNames": [
          "I01",
          "I02",
          "I03",
          "O01",
          "Scored Probabilities for Class \"0\"",
          "Scored Probabilities for Class \"1\"",
          "Scored Probabilities for Class \"2\"",
          "Scored Labels"
        ],
        "Values": [
          [
            "-0.96624",
            "0.02918",
            "-0.44237",
            null,
            "3.25456957391002E-12",
            "0.000107838139228988",
            "2.76633869589205E-07",
            "Y"
          ]
        ]
      }
    }
  }
}

Edit 2

If I print I get this JSON

Console.WriteLine(obj.Results.output1.value.Values);

[
    [
        "-0.96624",
        "0.02918",
        "-0.44237",
        null,
        "3.25456957391002E-12",
        "0.000107838139228988",
        "2.76633869589205E-07",
        "Y"
    ]
]

And the Count prints 1

Console.WriteLine(obj.Results.output1.value.Values.Count);

I almost there, I need the last element o the size to access by index in the inside array.

Edit 3

I could get the last element with:

Console.WriteLine(obj.Results.output1.value.Values[0].Last);

And the array size with:

Console.WriteLine(obj.Results.output1.value.Values[0].Count);
3 Answers
Related