J
J
just_clarity2022-03-16 12:36:43
C++ / C#
just_clarity, 2022-03-16 12:36:43

How to access nested data (json) correctly?

I have JSON like this:

spoiler
{
  "answer": "success",
  "data": [
    {
      "id": "1",
      "gn": "2",
      "mar": "3",
      "mo": "4",
      "mdf": "5"
    },
    {
      "id": "1",
      "gn": "2",
      "mar": "3",
      "mo": "4",
      "mdf": "5"
    }
  ]
}

There are automatically generated classes:
spoiler
public class Rootobject
    {
        public string answer { get; set; }
        public Datum[] data { get; set; }
    }

    public class Datum
    {
        public string id { get; set; }
        public string gn{ get; set; }
        public string mar { get; set; }
        public string mo { get; set; }
        public string mdf{ get; set; }
    }

I want to get data.gn, for example.
How to get answer I understand:
var test = JsonConvert.DeserializeObject<Rootobject>(answres);
System.Diagnostics.Debug.WriteLine(test.answer);

How can I access data.gn?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-03-16
@just_clarity

Obviously, test.data[0].gn
*yes, I forgot the index for the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question