I'm looking for a elaborated answer and/or explanation (with examples).
Q: The goal is to parse a JSON
file with the following format (example link provided). Only the url(s)
should be outputted to the console (using a array).
The expected output is
001
002
003
004
005
I was told repeatedly the answer is....
using System.Text.Json;
var json = "{ \"entries\": [ { \"value\": \"hello\", \"anotherValue\": 0 } ]"
var thing = JsonSerializer.Deserialize<Thing>(json);
foreach (var entry in thing.Entries) {
// do thing with entry.Value and entry.AnotherValue
}
record Thing(OtherThing[] Entries);
record OtherThing(string Value, int AnotherValue);
But I'm clueless. Though this is a proven method that work?
My progress:
internal class Thing
{
public class Download
{
public string sha1 { get; set; }
public string url { get; set; }
}
public class Root
{
public string _id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public string url { get; set; }
public List<Download> download { get; set; }
}
public void LoadJson()
{
using (StreamReader r = new StreamReader("test2json.json"))
{
string json = r.ReadToEnd();
Root file = JsonConvert.DeserializeObject<Root>(json);
Trace.WriteLine(json);
}
}
In a similar method...I was able to parse a JSON
url using...
//Parse JSON Directory.
var fileName = (@"C:\Users\Icarus\Desktop\test2json.json");
dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName));
string SearchFor = json["Name"]["Url"];
But this is not a desired result.
I'm using C# and the application uses WPF.