如何通过超链接C#从复杂的json中获取价值

时间:2018-11-29 18:42:26

标签: json

我的JSON是:

{
    "kind": "tm:ltm:pool:poolstats",
    "generation": 198990,
    "selfLink": "https://localhost/mgmt/tm/ltm/pool/~part~pool-1/stats?ver=12.1.2",
    "entries": {
        "https://localhost/mgmt/tm/ltm/pool/~part~pool-1/~part~pool-1/stats": {
            "nestedStats": {
                "kind": "tm:ltm:pool:poolstats",
                "selfLink": "https://localhost/mgmt/tm/ltm/pool/~part~pool-1/~part~pool-1/stats?ver=12.1.2",
                "entries": {
                    "activeMemberCnt": {
                        "value": 2
                    },
                    "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": "/Common/tcp"
                    },
                    "tmName": {
                        "description": "/part/pool-1"
                    },
                    "serverside.bitsIn": {
                        "value": 517432
                    },
                    "serverside.bitsOut": {
                        "value": 4378224
                    },
                    "**serverside.curConns**": {
                        "**value**": 0
                    },
                    "serverside.maxConns": {
                        "value": 16
                    },
                    "serverside.pktsIn": {
                        "value": 697
                    },
                    "serverside.pktsOut": {
                        "value": 697
                    },
                    "serverside.totConns": {
                        "value": 144
                    },
                    "status.availabilityState": {
                        "description": "available"
                    },
                    "status.enabledState": {
                        "description": "enabled"
                    },
                    "status.statusReason": {
                        "description": "The pool is available"
                    },
                    "totRequests": {
                        "value": 55
                    }
                }
            }
        }
    }
}

我想获取“ serverside.curConns”的“值”键的值。怎么样?

1 个答案:

答案 0 :(得分:0)

在不了解更多用例的情况下,下面是一个示例,说明如何使用JSON.NET C#通过其直接路径访问请求的令牌:

可运行的DotNetFiddle:

https://dotnetfiddle.net/yecPbV

为方便起见,以下粘贴了逻辑的内容:

var jsonObject = JObject.Parse(json);
var serverSideConnections = jsonObject.SelectToken("entries['https://localhost/mgmt/tm/ltm/pool/~part~pool-1/~part~pool-1/stats'].nestedStats.entries['serverside.curConns'].value");

Console.WriteLine("Current Connections: " + serverSideConnections.Value<int>());

上方json是json blob的字符串表示形式。