从网站读取嵌套Json并将所有值添加到ComboBox C#

5tmbdcev  于 2023-01-06  发布在  C#
关注(0)|答案(3)|浏览(141)

我正在为自己做一个应用程序,让我可以使Minecraft锻造服务器更容易,并下载服务器文件,我需要得到所有的版本,并允许用户选择他们想要的.所以,例如,我有这个Json从一个网站抓取:

{
  "homepage": "https://files.minecraftforge.net/net/minecraftforge/forge/",
  "promos": {
    "1.1-latest": "1.3.4.29",
    "1.2.3-latest": "1.4.1.64",
    "1.2.4-latest": "2.0.0.68",
    "1.2.5-latest": "3.4.9.171",
    "1.3.2-latest": "4.3.5.318",
    "1.4.0-latest": "5.0.0.326",
    "1.4.1-latest": "6.0.0.329",
    "1.4.2-latest": "6.0.1.355",
    "1.4.3-latest": "6.2.1.358",
    "1.4.4-latest": "6.3.0.378",
    "1.4.5-latest": "6.4.2.448",
    "1.4.6-latest": "6.5.0.489",
    "1.4.7-latest": "6.6.2.534",
    "1.5-latest": "7.7.0.598",
    "1.5.1-latest": "7.7.2.682",
    "1.5.2-latest": "7.8.1.738",
    "1.5.2-recommended": "7.8.1.738",
    "1.6.1-latest": "8.9.0.775",
    "1.6.2-latest": "9.10.1.871",
    "1.6.2-recommended": "9.10.1.871",
    "1.6.3-latest": "9.11.0.878"
  }
}

I want to take all the versions ex 1.1 and 1.2.3 and so on from the promos and add them to a ComboBox and then have a separate combobox for latest or recommended depending on what the version supports. Then I want to store an array of all the versions (ex 1.1: 1.3.4.29 or 1.2.3: 1.4.1.64)and their value for later. I have not messed with Jsons at all so this very possibly could be a very dumb question.
Photo of the app
我试过这样的方法:

using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                    var json = wc.DownloadString("https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json");
                    var parse = JObject.Parse(json);
                    var versions = parse.SelectToken("Data.ID.promos").Value<String>();

                    System.Diagnostics.Debug.WriteLine(versions);
                }

这是我从另一个堆栈溢出线程中找到的,但得到了以下错误:
抛出异常:Newtonsoft.Json.dll中的"系统参数为空异常"
应用程序崩溃了。
调试后,堆栈跟踪转到行
变量版本=解析.选择令牌("数据. ID. promos").值();
我检查了Json变量,它确实包含了整个Json。
链接到Json:https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json
谢谢你的帮助!

mpbci0fu

mpbci0fu1#

使用此代码获取版本:

System.Net.WebClient wc = new System.Net.WebClient();

       var json = wc.DownloadString("https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json");
       var versions = JObject.Parse(json)["promos"].ToString();
mrwjdhj3

mrwjdhj32#

作为JObject.Parse的替代方法,您可以使用JsonConvert.DeserializeObject
首先,定义一个与JSON文档结构相匹配的类,特别要注意的是,JSON的versions元素是一个名称-值对列表,其中所有的名称和值都是字符串,因此该元素可以反序列化为Dictionary<string, string>

public class SomeClass
{
    public string Homepage { get; set; }
    public Dictionary<string, string> Versions { get; set; }
}

用法:

var someClassInstance = JsonConvert.DeserializeObject<SomeClass>(json);
var versions = someClassInstance.Versions;
pkwftd7m

pkwftd7m3#

json中没有数据或ID。请尝试以下操作

KeyValuePair<string, string>[] versions = ((JObject)JObject.Parse(json)["promos"]).Properties()
 .Select(v => new KeyValuePair<string, string>(v.Name, (string)v.Value)).ToArray();

//or just a string array

string[] versions = ((JObject)JObject.Parse(json)["promos"]).Properties()
     .Select(v => v.Name + " - " + (string) v.Value ).ToArray();

 System.Diagnostics.Debug.WriteLine(string.Join("\n",versions));

您可以创建或选择任何其他类来代替KeyValuePair〈string,string〉

相关问题