Home > Enterprise >  Read nested Json from website and add all values to a ComboBox C#
Read nested Json from website and add all values to a ComboBox C#

Time:01-06

I am making a app for myself that allows me to make Minecraft Forge servers easier, and to download the server files I need to get all the versions and allow the user to select which one they would like. So for example I have this Json grabbed from a website:

{
  "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

I tried something like this:

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

which I found from another stack overflow thread but got the following error:

Exception thrown: 'System.ArgumentNullException' in Newtonsoft.Json.dll

and the app crashed.

After debugging the stack trace goes to the line

var versions = parse.SelectToken("Data.ID.promos").Value();

and I checked the Json variable and it does have the entire Json in it.

Link to the Json: https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json

Thanks for the help!

CodePudding user response:

Use this code to get versions:

       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();

CodePudding user response:

As an alternative to using JObject.Parse, you can use JsonConvert.DeserializeObject.

First, define a class which matches the structure of your JSON document. In particular, note that the versions element of the JSON is a list of name-value pairs, where all the names and all the values are strings, so that element can be deserialized into a Dictionary<string, string>.

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

Usage:

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

CodePudding user response:

there is not Data or ID in your json. Try this

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));

you can create or choose any another class instead of KeyValuePair<string, string>

  • Related