简体   繁体   中英

JsonValue.Parse not returning correct results

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

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/ .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM