https://stackoverflow.com/questions/47316985

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
					
public class Program
{
	public static void Main()
	{
		string json = MockAPI.GetJson();
		Statistics stats = JsonConvert.DeserializeObject<Statistics>(json);
		DumpStatistics(stats);
	}

	static void DumpStatistics(Statistics stats, string indent = "")
	{
		if (stats.Kind != null)
			Console.WriteLine(indent + "Kind: " + stats.Kind);
		if (stats.SelfLink != null)
			Console.WriteLine(indent + "SelfLink: " + stats.SelfLink);
		if (stats.Entries != null)
		{
			Console.WriteLine(indent + "Entries: ");
			indent = indent + "  ";
			foreach (var kvp in stats.Entries)
			{
				Console.Write(indent + kvp.Key + ": ");
				Entry entry = kvp.Value;
				switch (entry.EntryType)
				{
					case EntryType.NestedStats:
					Console.WriteLine();
					DumpStatistics(entry.NestedStats, indent + "  ");
					break;
					
					case EntryType.Description:
					Console.WriteLine(entry.Description);
					break;
					
					default:
					Console.WriteLine(entry.Value);
					break;
				}
			}
		}
	}
}

class Statistics
{
	[JsonProperty("entries")]
	public Dictionary<string, Entry> Entries { get; set; }

	[JsonProperty("kind")]
	public string Kind { get; set; }
	
	[JsonProperty("selfLink")]
	public string SelfLink { get; set; }
}

enum EntryType { Value, Description, NestedStats }

class Entry
{
	[JsonProperty("value")]
	public long Value { get; set; }
	
	[JsonProperty("description")]
	public string Description { get; set; }
	
	[JsonProperty("nestedStats")]
	public Statistics NestedStats { get; set; }
	
	public EntryType EntryType
	{
		get
		{
			if (NestedStats != null) return EntryType.NestedStats;
			if (Description != null) return EntryType.Description;
			return EntryType.Value;
		}
	}
}


class MockAPI
{
	public static string GetJson() 
	{
		return JSON;
	}
	
    private static readonly string JSON = @"
            {
              ""kind"": ""tm:ltm:pool:members:memberscollectionstats"",
              ""selfLink"": ""https://device/mgmt/tm/ltm/pool/myPoolName/members/stats?ver=12.1.2"",
              ""entries"": {
                ""https://device/mgmt/tm/ltm/pool/myPoolName/members/~Common~myPoolName/stats"": {
                  ""nestedStats"": {
                    ""kind"": ""tm:ltm:pool:members:membersstats"",
                    ""selfLink"": ""https://device/mgmt/tm/ltm/pool/myPoolName/members/~Common~myPoolName/stats?ver=12.1.2"",
                    ""entries"": {
                      ""activeMemberCnt"": {
                        ""value"": 16
                      },
                      ""connqAll.ageEdm"": {
                        ""value"": 0
                      },
                      ""connqAll.ageEma"": {
                        ""value"": 0
                      },
                      ""connqAll.ageHead"": {
                        ""value"": 0
                      },
                      ""connqAll.ageMax"": {
                        ""value"": 0
                      },
                      ""connqAll.depth"": {
                        ""value"": 0
                      },
                      ""connqAll.serviced"": {
                        ""value"": 0
                      },
                      ""connq.ageEdm"": {
                        ""value"": 0
                      },
                      ""connq.ageEma"": {
                        ""value"": 0
                      },
                      ""connq.ageHead"": {
                        ""value"": 0
                      },
                      ""connq.ageMax"": {
                        ""value"": 0
                      },
                      ""connq.depth"": {
                        ""value"": 0
                      },
                      ""connq.serviced"": {
                        ""value"": 0
                      },
                      ""curSessions"": {
                        ""value"": 0
                      },
                      ""minActiveMembers"": {
                        ""value"": 0
                      },
                      ""monitorRule"": {
                        ""description"": ""min 1 of /Common/prod-olbtp_https""
                      },
                      ""tmName"": {
                        ""description"": ""/Common/myPoolName""
                      },
                      ""serverside.bitsIn"": {
                        ""value"": 1854170442864
                      },
                      ""serverside.bitsOut"": {
                        ""value"": 28010721155520
                      },
                      ""serverside.curConns"": {
                        ""value"": 46
                      },
                      ""serverside.maxConns"": {
                        ""value"": 350
                      },
                      ""serverside.pktsIn"": {
                        ""value"": 1486804119
                      },
                      ""serverside.pktsOut"": {
                        ""value"": 2494389460
                      },
                      ""serverside.totConns"": {
                        ""value"": 2864936
                      },
                      ""status.availabilityState"": {
                        ""description"": ""available""
                      },
                      ""status.enabledState"": {
                        ""description"": ""enabled""
                      },
                      ""status.statusReason"": {
                        ""description"": ""The pool is available""
                      },
                      ""totRequests"": {
                        ""value"": 0
                      },
                      ""https://device/mgmt/tm/ltm/pool/myPoolName/members/~Common~myPoolName/members/stats"": {
                        ""nestedStats"": {
                          ""entries"": {
                            ""https://device/mgmt/tm/ltm/pool/myPoolName/members/~Common~myPoolName/members/~Common~10.10.10.30:443/stats"": {
                              ""nestedStats"": {
                                ""entries"": {
                                  ""addr"": {
                                    ""description"": ""10.10.10.30""
                                  },
                                  ""connq.ageEdm"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageEma"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageHead"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageMax"": {
                                    ""value"": 0
                                  },
                                  ""connq.depth"": {
                                    ""value"": 0
                                  },
                                  ""connq.serviced"": {
                                    ""value"": 0
                                  },
                                  ""curSessions"": {
                                    ""value"": 0
                                  },
                                  ""monitorRule"": {
                                    ""description"": ""min 1 of /Common/prod-olbtp_https (pool monitor)""
                                  },
                                  ""monitorStatus"": {
                                    ""description"": ""up""
                                  },
                                  ""nodeName"": {
                                    ""description"": ""/Common/10.10.10.30""
                                  },
                                  ""poolName"": {
                                    ""description"": ""/Common/myPoolName""
                                  },
                                  ""port"": {
                                    ""value"": 443
                                  },
                                  ""serverside.bitsIn"": {
                                    ""value"": 123606959904
                                  },
                                  ""serverside.bitsOut"": {
                                    ""value"": 1252550265608
                                  },
                                  ""serverside.curConns"": {
                                    ""value"": 4
                                  },
                                  ""serverside.maxConns"": {
                                    ""value"": 18
                                  },
                                  ""serverside.pktsIn"": {
                                    ""value"": 75556262
                                  },
                                  ""serverside.pktsOut"": {
                                    ""value"": 115991280
                                  },
                                  ""serverside.totConns"": {
                                    ""value"": 189971
                                  },
                                  ""sessionStatus"": {
                                    ""description"": ""enabled""
                                  },
                                  ""status.availabilityState"": {
                                    ""description"": ""available""
                                  },
                                  ""status.enabledState"": {
                                    ""description"": ""enabled""
                                  },
                                  ""status.statusReason"": {
                                    ""description"": ""Pool member is available""
                                  },
                                  ""totRequests"": {
                                    ""value"": 0
                                  }
                                }
                              }
                            },
                            ""https://device/mgmt/tm/ltm/pool/myPoolName/members/~Common~myPoolName/members/~Common~10.10.10.40:443/stats"": {
                              ""nestedStats"": {
                                ""entries"": {
                                  ""addr"": {
                                    ""description"": ""10.10.10.40""
                                  },
                                  ""connq.ageEdm"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageEma"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageHead"": {
                                    ""value"": 0
                                  },
                                  ""connq.ageMax"": {
                                    ""value"": 0
                                  },
                                  ""connq.depth"": {
                                    ""value"": 0
                                  },
                                  ""connq.serviced"": {
                                    ""value"": 0
                                  },
                                  ""curSessions"": {
                                    ""value"": 0
                                  },
                                  ""monitorRule"": {
                                    ""description"": ""min 1 of /Common/prod-olbtp_https (pool monitor)""
                                  },
                                  ""monitorStatus"": {
                                    ""description"": ""up""
                                  },
                                  ""nodeName"": {
                                    ""description"": ""/Common/10.10.10.40""
                                  },
                                  ""poolName"": {
                                    ""description"": ""/Common/myPoolName""
                                  },
                                  ""port"": {
                                    ""value"": 443
                                  },
                                  ""serverside.bitsIn"": {
                                    ""value"": 70383109304
                                  },
                                  ""serverside.bitsOut"": {
                                    ""value"": 1114184738168
                                  },
                                  ""serverside.curConns"": {
                                    ""value"": 3
                                  },
                                  ""serverside.maxConns"": {
                                    ""value"": 32
                                  },
                                  ""serverside.pktsIn"": {
                                    ""value"": 58376213
                                  },
                                  ""serverside.pktsOut"": {
                                    ""value"": 98729511
                                  },
                                  ""serverside.totConns"": {
                                    ""value"": 103410
                                  },
                                  ""sessionStatus"": {
                                    ""description"": ""enabled""
                                  },
                                  ""status.availabilityState"": {
                                    ""description"": ""available""
                                  },
                                  ""status.enabledState"": {
                                    ""description"": ""enabled""
                                  },
                                  ""status.statusReason"": {
                                    ""description"": ""Pool member is available""
                                  },
                                  ""totRequests"": {
                                    ""value"": 0
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }";
}
;