Reputation: 23
I am trying to parse a .Json file which can be found here - http://files.star-made.org/releasebuildindex.json with this snippet I found on this site -
WebClient webClient = new WebClient();
dynamic result = JsonValue.Parse(new WebClient().DownloadString("http://files.star-made.org/releasebuildindex.json"));
Console.WriteLine(result.builds.version);
With the current version of the file (changes every week or 2 with a game update), it should be returning the string "0.163", yet it currently returns "default", which after some messing around, is what it returns if that tag does not actually exist.
I have no knowledge of Json, and can not edit that file as I am not its creator, any assistance is greatly appreciated.
-James
Upvotes: 0
Views: 398
Reputation: 20028
Dynamic keyword is nice, but it is way better to have static code checking and auto-complete. To enable this you need to have POCO(Plain Old CLR(Common Language Runtime) Object).
Copy Json and Paste special -> Paste as json classes (you need at least Visual studio 2012.2 RC):
public class Rootobject
{
public string indexfileversion { get; set; }
public string type { get; set; }
public Build[] builds { get; set; }
}
public class Build
{
public string version { get; set; }
public string build { get; set; }
public string rawVersion { get; set; }
public string path { get; set; }
}
Then you can use :
var json = new WebClient()
.DownloadString("http://files.star-made.org/releasebuildindex.json");
var result = new JavaScriptSerializer().Deserialize<Rootobject>(json);
Console.WriteLine(result.builds[0].version);
Upvotes: 1
Reputation: 5904
Builds is an array. You get the version like this:
Console.WriteLine(results.builds[0].version);
To explore the structure of a json string you can use http://json2csharp.com/.
Upvotes: 1