简体   繁体   English

JsonValue.Parse没有返回正确的结果

[英]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 - 我试图解析这可以在这里找到一个以.json文件- http://files.star-made.org/releasebuildindex.json在这个片段我在此网站上发现-

        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. 使用文件的当前版本(每周更新一次,或每2个游戏更新更新一次),它应该返回字符串“ 0.163”,但当前返回的是“默认”,经过一番摸索之后,如果该标签返回了该值实际上不存在。

I have no knowledge of Json, and can not edit that file as I am not its creator, any assistance is greatly appreciated. 我不了解Json,并且不能编辑该文件,因为我不是它的创建者,非常感谢您的协助。

-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/ . 要探究json字符串的结构,可以使用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). 为此,您需要具有POCO(普通旧CLR(公共语言运行时)对象)。

Copy Json and Paste special -> Paste as json classes (you need at least Visual studio 2012.2 RC ): 复制Json和Paste special-> Paste作为json类 (您至少需要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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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