我如何拿一个JSON字符串并选择一个随机条目并从该条目获取变量
我有以下JSON ..
{
"Followers": [{
"ID": 0,
"Username": "nutty",
"Game": "Just Chatting",
"Viewers": 200,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nutty-1920x1080.jpg"
}, {
"ID": 1,
"Username": "CloneKorp",
"Game": "Software and Game Development",
"Viewers": 31,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_clonekorp-1920x1080.jpg"
}, {
"ID": 2,
"Username": "kingswarrior9953",
"Game": "Art",
"Viewers": 1,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_kingswarrior9953-1920x1080.jpg"
}]
}
我想做类似的事情。
JObject data = JObject.Parse(json);
int SelectedViewers = data["Followers"][1]["Viewers"];
在哪里可以抓住第二个条目(1个条目的ID)并将“观众”的变量设置为31。该数字将是基于随机数的随机数根据所有条目的数量,但我还没有。
但是,这似乎不起作用。关于这里破裂的内容有什么想法吗?
I have the following json..
{
"Followers": [{
"ID": 0,
"Username": "nutty",
"Game": "Just Chatting",
"Viewers": 200,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nutty-1920x1080.jpg"
}, {
"ID": 1,
"Username": "CloneKorp",
"Game": "Software and Game Development",
"Viewers": 31,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_clonekorp-1920x1080.jpg"
}, {
"ID": 2,
"Username": "kingswarrior9953",
"Game": "Art",
"Viewers": 1,
"Image": "https://static-cdn.jtvnw.net/previews-ttv/live_user_kingswarrior9953-1920x1080.jpg"
}]
}
I'd like to do something like..
JObject data = JObject.Parse(json);
int SelectedViewers = data["Followers"][1]["Viewers"];
Where it would grab the second entry (the ID of 1 entry) and set the variable of "Viewers" to 31. The number would be a random number based on the count of all the entries, but I'm not to that point yet.
However, this doesn't seem to work. Any ideas on what is broken here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里缺少铸造:
以上应该有效。
You are missing casting here:
The above should work.
尝试一下
try this