0

I'm trying to extract specific data, in this case "Name" from a JSON file so that I can use it for the rest of my code later.

My current code where I load JSON:

public static void LoadJson()
{
    using (StreamReader r = new StreamReader(@"C:\Users\Work\Documents\MyJson.json"))
    {
        string json = r.ReadToEnd();
        var o = JsonConvert.DeserializeObject<JObject>(json);
        var h = o.Value<JObject>("Data")
            .Value<JArray>("Accounts");
        Console.WriteLine(h[0]);

    }
}

JSON example:

{
  "Data": {
    "Accounts": [
      {
        "Name": "owner",
        "Address": "123456"
      },
      {
        "Name": "Lerris",
        "Address": "179672"
      }
    ]
  }
}

The output I get from my code right now:

enter image description here

JSON length is much longer than the one I posted above, but it was just to make an example.

Question: How do I obtain and define both "Name" and "Address" value so that I can use them for the rest of my code?

I hope you guys will get what I mean. If I weren't specific enough please just say and I'll try to explain again.

Kali
  • 39
  • 3
  • I'm trying to define each one of them (Name and Address) in the code – Kali Dec 03 '20 at 22:58
  • I've tried with get set but i couldnt really get it to work – Kali Dec 03 '20 at 23:08
  • 1
    The output you say you are getting from your code doesn't match the example JSON you posted. How does Username and Password relate to Name and Address? Is the JSON right? – Brian Rogers Dec 03 '20 at 23:41
  • Oh, my bad I was using another JSON with the same structure. I edited the screenshot again to fit with the JSON example I posted – Kali Dec 03 '20 at 23:45

1 Answers1

0

See this section:

Use JsonDocument for access to data

Julio Cachay
  • 780
  • 5
  • 10