asp.net 从jToken获取值

dced5bon  于 2022-12-20  发布在  .NET
关注(0)|答案(2)|浏览(185)

我情况是这样的:
我将JSON字符串解析为JObject。

JObject jObject = JObject.Parse(json);

跳转到JSON中的“whitelist”标记

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"
        }
    }
}
0qx6xfy6

0qx6xfy61#

我将把你的 whitelist 转换成字典,你可以很容易地用它来得到你想要的字符串

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"]
                });
ldioqlga

ldioqlga2#

埃塞尔的答案当然是好的,但一个更简化的答案可能如下:

var jTokenProperties = jToken.Children().OfType<JProperty>();
foreach (JProperty property in jTokenProperties)
{
    var key = property.Name;
    var value = property.Value;
}

其中jToken被假定为现有的JToken对象。
诀窍是使用JProperty,首先将Children()转换为可枚举的JProperty

相关问题