读取 Json 值

我正在使用下面的代码来获取 json 的值


using static ScriptParser.JsonData;


namespace ScriptParser

{

    class Program

    {

        static void Main(string[] args)

        {


            var url = "https://my-json-server.typicode.com/Subusam/demojson/db";

            //var ScriptDetails = _download_serialized_json_data<Rootobject>(url);


           // Console.WriteLine(ScriptDetails);

            var ScriptDetails = _download_serialized_json_data<Rootobject>(url);

            Console.WriteLine(ScriptDetails);

        }


    }

}

使用下面的代码反序列化 json


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using Newtonsoft.Json;


namespace ScriptParser

{

    class JsonData

    {

        public static JasonRaw _download_serialized_json_data<JasonRaw>(string url) where JasonRaw : new()

        {

            using (var w = new WebClient())

            {

                var json_data = string.Empty;

                // attempt to download JSON data as a string

                try

                {

                    json_data = w.DownloadString(url);

                }

                catch (Exception) { }

                // if string with JSON data is not empty, deserialize it to class and return its instance 

                return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<JasonRaw>(json_data) : new JasonRaw();

            }

        }


        public class Rootobject

        {

            public execution[] execution { get; set; }

            public scenarios scenarios { get; set; }


        }


        public class scenarios

        {

            public threadGroup threadGroup { get; set; }

        }


        public class threadGroup

        {

            public request[] requests { get; set; }

        }

现在我想提取像“method”、“url”、“concurrency”、“hold-for”、“ramp-up”这样的值。在我的代码中需要帮助,因为我面临获取值的问题。运行此代码时,我没有得到“method”和“url”的值。我附上了运行代码时获得的价值

http://img.mukewang.com/61da81b00001455509990441.jpg

婷婷同学_
浏览 250回答 1
1回答

小怪兽爱吃肉

问题是类中的属性名称与 JSON 中的名称不匹配。使用JsonProperty属性来指定。例如execution类:public class execution{&nbsp; &nbsp; public int concurrency { get; set; }&nbsp; &nbsp; [JsonProperty("hold-for")]&nbsp; &nbsp; public string holdfor { get; set; }&nbsp; &nbsp; [JsonProperty("ramp-up")]&nbsp; &nbsp; public string rampup { get; set; }&nbsp; &nbsp; public string scenario { get; set; }}并且scenarios做:public class scenarios{&nbsp; &nbsp; [JsonProperty("Thread Group")]&nbsp; &nbsp; public threadGroup threadGroup { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP