我正在使用下面的代码来获取 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); } }}使用下面的代码反序列化 jsonusing 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”的值。我附上了运行代码时获得的价值
1 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
问题是类中的属性名称与 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; }
}
- 1 回答
- 0 关注
- 177 浏览
添加回答
举报
0/150
提交
取消