0

I am querying an API and returning data to a C# Console App. I am able to query my data successfully except for the array element of such. This is how the data is returned in JSON format

[
  {
    "UserType": "Admin",
    "UserPerms:" [
        {
            "level1:" "Yes",
            "level2:" "No", 
            "level3:" "No",
            "level4:" "Yes",
            "level5:" [
              {
                "door1:" "Yes",
                "door2:" "No", 
                "door3:" "No",
                "doory4:" "Yes"
              }
            ]
        }
    ]
  }         
]

And this is the C# syntax that I am trying to use to return the data - what is the proper C# syntax to return each door value for level5?

public class RootObject
{
    public string UserType { get; set; }
    public List<UserPerms> UserPerms { get; set; }
}
public class UserPerms
{
    public string level1 { get; set; }
    public string level2 { get; set; }
    public string level3 { get; set; }
    public string level4 { get; set; }
    public string[] level5 { get; set; }
}

public class Main[]
{
    var response = syncClient.DownloadString(url);
    var o = JsonConvert.DeserializeObject<RootObject[]>(response);
    foreach (RootObject ro in o)
        if (ro.UserPerms != null)
            foreach (UserPerms info in ro.UserPerms)
            {
                Console.WriteLine("Access to Level1" + info.level1);
                Console.WriteLine("Access to Level2" + info.level2);
                Console.WriteLine("Access to Level3" + info.level3);
                Console.WriteLine("Access to Level4" + info.level4);
                Console.WriteLine("Access to Level5" + info.level5);
            }
}
7
  • 1
    What is the intent of the pattern where you make everything an array with one object in it? Commented Jun 12, 2017 at 18:40
  • @EdPlunkett - for level5? Or the overall syntax? If the overall syntax - this was inherited from someone and the syntax works so I went with it... Commented Jun 12, 2017 at 18:43
  • It's a strange structure that appears to unnecessarily complicate things. Commented Jun 12, 2017 at 18:44
  • @Amy - how would you recommend the structure be set? Commented Jun 12, 2017 at 18:53
  • Why don't you follow the pattern as complicated as it looks/becomes? A new class for level5 like UserPerms and then declare a List<Level5> in UserPerms. Commented Jun 12, 2017 at 18:57

1 Answer 1

1

The JSON for level5 displays a "list" of "key-value pair", so....

public List<Dictionary<string, string>> level5 { get; set; }

I'll try to come up with a fiddle soon.

Sign up to request clarification or add additional context in comments.

6 Comments

how would I then write the results to the Console?
info.level5.door... info.level5.door2... and so on
that gives an error of 'List<Dictionary<string,string>>' does not contain a definition for 'door1' and no extension method of 'door1' accepting a first argument of type 'List<Dictionary<string,string>>' When I try to use Console.WriteLine("Access to Level5" + info.level5.door1);
You are declaring the array string in your main method - how would I declare it since the results are beingn returned from an API?
replace the line with "var json....." with "var response = syncClient.DownloadString(url);"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.