从jToken获取值

时间:2018-06-20 20:41:07

标签: c# asp.net

这是我的情况:

我有一个解析为JObject的JSON字符串。

JObject jObject = JObject.Parse(json);

我跳到JSON中的“白名单”令牌

JToken token = jObject.SelectToken("whitelist");

我想遍历我的白名单(子级)值,进入子级并将我的值发送/发送到文本框。循环很好,但是如何读出x的值并将其放入字符串或发送至文本框?

foreach (JToken x in token)
{
   //Want my values from x!
} 

使用JObject可以像这样完成它,但是我希望它可以与JToken一起使用:

foreach (var x in jObject)
{
    string bulbno = x.Key;
    JToken obj = x.Value;
    dictionaryBulbs.Add(bulbno, (string)obj["name"]);
} 

我的JSON输入:

{
    "name": "Philips hue",
    "zigbeechannel": 20,
    "bridgeid": "001788FFFE4957F4",
    "mac": "00:17:88:49:57:f4",
    "dhcp": true,
    "ipaddress": "192.168.0.50",
    "netmask": "255.255.255.0",
    "gateway": "192.168.0.254",
    "proxyaddress": "none",
    "proxyport": 0,
    "UTC": "2018-06-20T16:52:16",
    "localtime": "2018-06-20T18:52:16",
    "timezone": "Europe/Amsterdam",
    "modelid": "BSB002",
    "datastoreversion": "70",
    "swversion": "1804201116",
    "apiversion": "1.25.0",
    "swupdate": {
        "updatestate": 0,
        "checkforupdate": false,
        "devicetypes": {
            "bridge": false,
            "lights": [],
            "sensors": []
        },
        "url": "",
        "text": "",
        "notify": true
    },
    "swupdate2": {
        "checkforupdate": false,
        "lastchange": "2018-06-09T17:31:24",
        "bridge": {
            "state": "noupdates",
            "lastinstall": "2018-05-17T19:45:47"
        },
        "state": "noupdates",
        "autoinstall": {
            "updatetime": "T14:00:00",
            "on": false
        }
    },
    "linkbutton": false,
    "portalservices": true,
    "portalconnection": "connected",
    "portalstate": {
        "signedon": true,
        "incoming": false,
        "outgoing": true,
        "communication": "disconnected"
    },
    "internetservices": {
        "internet": "connected",
        "remoteaccess": "connected",
        "time": "connected",
        "swupdate": "connected"
    },
    "factorynew": false,
    "replacesbridgeid": null,
    "backup": {
        "status": "idle",
        "errorcode": 0
    },
    "starterkitid": "",
    "whitelist": {
        "1buaoU3xjmyKgnakr5ci4bw3HxUJkc5xjhROSqJ-": {
            "last use date": "2018-06-20T16:45:44",
            "create date": "2017-03-18T22:26:42",
            "name": "Hue 2#Samsung SM-G930F"
        },
        "57kRCjXnD5jnEv4cBcoWt5bmHZigTUNVnV5IBjal": {
            "last use date": "2017-03-19T16:30:36",
            "create date": "2017-03-19T16:19:21",
            "name": "HueDiscoBridgeConnec#SM-G930F"
        },
        "V4ritG8iH79Isyvp3tv6SZaJsmQ3gWd7yRMCpYiQ": {
            "last use date": "2018-05-28T13:30:45",
            "create date": "2017-03-19T16:22:18",
            "name": "Hue 2#Samsung SM-G925F"
        },
        "qwonzSD2YDp4ApzYxcvJ16nVkVxiqryVYvcUv72G": {
            "last use date": "2018-06-19T14:30:44",
            "create date": "2017-03-19T20:38:23",
            "name": "HueDiscoBridgeConnec#SM-G930F"
        },
        "4603-KtV5WCh1VrMuJmmGXjXxYuwmw3W8L8UPe9P": {
            "last use date": "2017-04-20T21:11:28",
            "create date": "2017-03-25T21:34:03",
            "name": "OnSwitch#SM-G930F"
        },
        "990Pgq8dEaaY2ciaiMRZMtdTeYNSyjtE06GLSEfw": {
            "last use date": "2018-05-25T16:45:45",
            "create date": "2017-03-26T19:20:18",
            "name": "all 4 hue#SM-G930F"
        },
        "-7eAHxy45PsR6PNMR0hO-ykwXrEZBXtxApJBmUZQ": {
            "last use date": "2017-03-26T20:16:59",
            "create date": "2017-03-26T20:16:59",
            "name": "HueNest#WebService"
        },
        "bWZViDh0GkhiulzdtjrOjZNkn5LhYr-Z5ZmOXAXh": {
            "last use date": "2017-03-26T20:17:23",
            "create date": "2017-03-26T20:17:23",
            "name": "HueNest#WebService"
        },
        "LXDX8UiFtlbjNg7nvlt6-qUFN2LpZ-N4S4tybmE0": {
            "last use date": "2018-02-10T15:27:09",
            "create date": "2017-03-26T20:17:27",
            "name": "HueNest#WebService"
        },
        "81YsJYmv08gDB36W52H0WWGe2aZttE06uAWgQByl": {
            "last use date": "2017-03-26T20:39:20",
            "create date": "2017-03-26T20:25:59",
            "name": "iftttv2"
        },
        "lzQiOJsWQ5sVBMuIlaLzxO3KOw8VE-rODJrU2c6b": {
            "last use date": "2017-03-26T20:39:20",
            "create date": "2017-03-26T20:28:00",
            "name": "iftttv2"
        },
        "GaBE6-zTFTgnixXmsCCbJsITnArJRWRnVt6xNwRW": {
            "last use date": "2018-01-26T21:50:57",
            "create date": "2017-11-03T21:03:46",
            "name": "talpa_the_voice#8e2e570e558d"
        },
        "u8kj2jktLZSQrM8etUQ2PKk1y8OkaHuX0pnD610t": {
            "last use date": "2018-04-03T20:46:52",
            "create date": "2018-04-03T17:58:16",
            "name": "philippeshueremote"
        },
        "2g5bkkfvTARp3BSXNPfXOZKftXuM2-0CkvN98OqF": {
            "last use date": "2018-06-19T22:00:44",
            "create date": "2018-05-29T16:09:03",
            "name": "Hue 2#Samsung SM-A530F"
        },
        "m-bk9APazTly1qanNxFCPmxCCyDfjEfM42nVbOqF": {
            "last use date": "2018-06-20T16:52:16",
            "create date": "2018-06-13T18:27:45",
            "name": "my_hue_app#test mart"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我会将您的白名单转换为字典。您可以轻松地使用它来获取所需的字符串

b