如何从jToken获取值?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (3303)

以下是我的情况:

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

JObject jObject = JObject.Parse(json);

我跳到JSON中的“白名单”标记

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

我想循环遍历我的白名单(子)值,进入Childs并获取/发送我的值到一个文本框中。循环很好,但是如何读取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"
        }
    }
}
提问于
用户回答回答于

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

var jObj= JObject.Parse(jsonstring);
var result = jObj.SelectToken("whitelist").Children().OfType<JProperty>()
                .ToDictionary(p => p.Name, p=> new
                {
                    Created = (DateTime)p.First()["create date"],
                    Name = (string)p.First()["name"]
                });

扫码关注云+社区

领取腾讯云代金券