读取Json值

时间:2018-09-08 10:37:27

标签: c# json

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

action_button_confirm

要反序列化以下代码,请使用

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

       public class request
        {
            public string label { get; set; }
            public string method { get; set; }
            public string url { get; set; }
        }

        public class execution
        {
            public int concurrency { get; set; }
            [JsonProperty(PropertyName = "hold-for")]
            public string holdfor { get; set; }
            [JsonProperty(PropertyName = "ramp-up")]
            public string rampup { get; set; }
            public string scenario { get; set; }
        }     

    }
}

现在,我想提取诸如“ method”,“ url”,“ concurrency”,“ hold-for”,“ ramp-up”之类的值。由于遇到获取值的问题,需要我的代码提供帮助。 在运行此代码时,我没有得到“ method”和“ url”的值。 我已经附上了运行代码时获得的价值所在

1 个答案:

答案 0 :(得分:3)

问题在于类中的属性名称与JSON中的属性名称不匹配。使用JsonProperty属性进行指定。

例如execution类的

public class execution
{
    public int concurrency { get; set; }
    [JsonProperty("hold-for")]
    public string holdfor { get; set; }
    [JsonProperty("ramp-up")]
    public string rampup { get; set; }
    public string scenario { get; set; }
}

对于scenarios,请执行以下操作:

public class scenarios
{
    [JsonProperty("Thread Group")]
    public threadGroup threadGroup { get; set; }
}
相关问题