如何呼叫 Wrapper 項中的值。我認為它會是例如 items.SysName 但沒有運氣。我正在呼叫一個查詢,它回傳一個 json 格式的文本串列,然后使用 jsonConvert 過濾我只是無法呼叫結果,但我可以查看它們在除錯器中。最終結果是我將創建一個 SysName 值串列并回傳所述串列。
public async Task<string> Table()
{
try
{
using (HttpClient client = GetClient())
{
var response = client.GetAsync("unimportant");
response.Wait();
if (response.Result.IsSuccessStatusCode)
{
var result = await response.Result.Content.ReadAsStringAsync();
Wrapper items = JsonConvert.DeserializeObject<Wrapper>(result);
return null;
}
};
} catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to retrieve requested Table |{ex.Message}");
Console.WriteLine(ex.Message);
}
return null;
}
包裝類
public class Question
{
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class SysPackage
{
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class SysScope
{
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class Result
{
[JsonProperty("rec_misc")]
public string RecMisc { get; set; }
[JsonProperty("question")]
public Question Question { get; set; }
[JsonProperty("sys_mod_count")]
public string SysModCount { get; set; }
[JsonProperty("sys_updated_on")]
public string SysUpdatedOn { get; set; }
[JsonProperty("sys_tags")]
public string SysTags { get; set; }
[JsonProperty("sys_class_name")]
public string SysClassName { get; set; }
[JsonProperty("published_ref")]
public string PublishedRef { get; set; }
[JsonProperty("sys_id")]
public string SysId { get; set; }
[JsonProperty("sys_package")]
public SysPackage SysPackage { get; set; }
[JsonProperty("inactive")]
public string Inactive { get; set; }
[JsonProperty("sys_update_name")]
public string SysUpdateName { get; set; }
[JsonProperty("sys_updated_by")]
public string SysUpdatedBy { get; set; }
[JsonProperty("sys_created_on")]
public string SysCreatedOn { get; set; }
[JsonProperty("sys_name")]
public string SysName { get; set; }
[JsonProperty("sys_scope")]
public SysScope SysScope { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("sys_created_by")]
public string SysCreatedBy { get; set; }
[JsonProperty("misc")]
public string Misc { get; set; }
[JsonProperty("order")]
public string Order { get; set; }
[JsonProperty("sys_policy")]
public string SysPolicy { get; set; }
}
public class Wrapper
{
[JsonProperty("result")]
public IList<Result> Result { get; set; }
}
}
顯示結果的影像確實出現了。

uj5u.com熱心網友回復:
items是一個Wrapper包含 : 的Result 集合items.Result。
您可以遍歷它以逐一訪問結果。一種方法是使用 foreach 回圈
foreach (Result result in items.Result)
{
string sysName = result.SysName;
}
您還可以使用 LinQ 檢索所有 sysNames
IEnumerable<string> sysNames = items.Result.Select(result => result.SysName);
如果您只關心第一個結果,只需使用
Result result = items.Result.First();
string sysName = result.SysName;
PS:重命名Wrapper.Result為Wrapper.Results和var items = ...tovar wrapper = ... 更明確地說明您的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483055.html
