gpt4 book ai didi

c# - 从 jToken 获取值

转载 作者:行者123 更新时间:2023-12-02 14:59:25 26 4
gpt4 key购买 nike

这是我的情况:

我有一个解析为 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"
}
}
}

最佳答案

我会将您的白名单 转换为字典。你可以很容易地使用它来得到你想要的字符串

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

关于c# - 从 jToken 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com