我如何拿一个JSON字符串并选择一个随机条目并从该条目获取变量

发布于 2025-02-13 02:31:17 字数 950 浏览 2 评论 0原文

我有以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2025-02-20 02:31:17

您在这里缺少铸造:

int SelectedViewers = Int32.Parse((string)data["Followers"][1]["Viewers"]);

以上应该有效。

You are missing casting here:

int SelectedViewers = Int32.Parse((string)data["Followers"][1]["Viewers"]);

The above should work.

鼻尖触碰 2025-02-20 02:31:17

尝试一下

   using Newtonsoft.Json;
   using Newtonsoft.Json.Linq;

    var followers = (JArray)JObject.Parse(json)["Followers"];
    var id=1;
    
    int selectedViewers = followers.Where(f=> (int)f["ID"]==id)
                                   .Select(f => (int) f["Viewers"])
                                   .FirstOrDefault();  //31

try this

   using Newtonsoft.Json;
   using Newtonsoft.Json.Linq;

    var followers = (JArray)JObject.Parse(json)["Followers"];
    var id=1;
    
    int selectedViewers = followers.Where(f=> (int)f["ID"]==id)
                                   .Select(f => (int) f["Viewers"])
                                   .FirstOrDefault();  //31
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文