Json DeserializedObject 如何调用返回值
Json DeserializedObject how do I call the returned values
如何调用包装器项目中的值。我假设它会是例如 items.SysName 但没有运气。我正在调用一个查询,其中 return 是 json 格式的文本列表,然后使用 jsonConvert 进行过滤 我只是无法调用结果,但我可以看到它们在那里在调试器中。最终结果是我将创建一个 SysName 值列表和 return 所述列表。
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;
}
包装器class
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; }
}
}
显示结果的图片。
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 = ...
重命名为 var wrapper = ...
可以更明确地说明您的数据。
如何调用包装器项目中的值。我假设它会是例如 items.SysName 但没有运气。我正在调用一个查询,其中 return 是 json 格式的文本列表,然后使用 jsonConvert 进行过滤 我只是无法调用结果,但我可以看到它们在那里在调试器中。最终结果是我将创建一个 SysName 值列表和 return 所述列表。
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;
}
包装器class
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; }
}
}
显示结果的图片。
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 = ...
重命名为 var wrapper = ...
可以更明确地说明您的数据。