过滤来自 API 次调用的 JSON 响应
Filter JSON response from an API call
基本上我正在打一个 api 电话,我收到 api 格式的 JSON 电话,我基本上需要过滤 api 电话到 return sys_name 的值。我不确定如何完成此操作,因为我的 JsonConvert.DeserializeObject<>() 返回空白。
{
try
{
using (HttpClient client = GetClient())
{
var response = client.GetAsync("this is the api call but I cleared it as you wont be able to use anyway");
response.Wait();
if (response.Result.IsSuccessStatusCode)
{
var result = await response.Result.Content.ReadAsStringAsync();
Result items = JsonConvert.DeserializeObject<Result>(result);
Console.WriteLine(items.SysName);
return null;
}
};
} catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to retrieve requested Table |{ex.Message}");
Console.WriteLine(ex.Message);
}
return null;
}
然后我有一个包装器 class,我使用 jsonutils.com
{
[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; }
}
}
Showing that items comes up empty
JSON 格式我不得不编辑一些值并观察它的方式更长,但这应该给出一个想法
{
"result": [
{
"rec_misc": "0",
"question": {
"link": "link goes here",
"value": "494a2"
},
"sys_mod_count": "1",
"sys_updated_on": "2021-11-15 17:18:10",
"sys_tags": "",
"sys_class_name": "question_choice",
"published_ref": "",
"sys_id": "",
"sys_package": {
"link": "link goes here",
"value": "global"
},
"inactive": "false",
"sys_update_name": "question_choice_",
"sys_updated_by": "someone ",
"sys_created_on": "2021-11-15 17:17:35",
"sys_name": "BG Info",
"sys_scope": {
"link": "link goes here",
"value": "global"
},
"text": "BG Info",
"value": "BG Info",
"sys_created_by": "someone",
"misc": "0",
"order": "300",
"sys_policy": ""
},
{
"rec_misc": "0",
"question": {
"link": "link goes here",
"value": "5b42"
},
"sys_mod_count": "0",
"sys_updated_on": "2021-04-14 06:56:39",
"sys_tags": "",
"sys_class_name": "question_choice",
"published_ref": "",
"sys_id": "05c2d",
"sys_package": {
"link": "link goes here",
"value": "global"
},
"inactive": "false",
"sys_update_name": "question_choice_",
"sys_updated_by": "someone",
"sys_created_on": "2021-04-14 06:56:39",
"sys_name": "Baseline (Developer Plus Build Mgr)",
"sys_scope": {
"link": "link goes here",
"value": "global"
},
"text": "Baseline (Developer Plus Build Mgr)",
"value": "baseline",
"sys_created_by": "someone",
"misc": "0",
"order": "300",
"sys_policy": ""
},
使用
Wrapper results = JsonConvert.DeserializeObject<Wrapper>(result);
响应以数组形式出现,因此您必须正确映射数据。
基本上我正在打一个 api 电话,我收到 api 格式的 JSON 电话,我基本上需要过滤 api 电话到 return sys_name 的值。我不确定如何完成此操作,因为我的 JsonConvert.DeserializeObject<>() 返回空白。
{
try
{
using (HttpClient client = GetClient())
{
var response = client.GetAsync("this is the api call but I cleared it as you wont be able to use anyway");
response.Wait();
if (response.Result.IsSuccessStatusCode)
{
var result = await response.Result.Content.ReadAsStringAsync();
Result items = JsonConvert.DeserializeObject<Result>(result);
Console.WriteLine(items.SysName);
return null;
}
};
} catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to retrieve requested Table |{ex.Message}");
Console.WriteLine(ex.Message);
}
return null;
}
然后我有一个包装器 class,我使用 jsonutils.com
{
[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; }
}
}
Showing that items comes up empty
JSON 格式我不得不编辑一些值并观察它的方式更长,但这应该给出一个想法
{
"result": [
{
"rec_misc": "0",
"question": {
"link": "link goes here",
"value": "494a2"
},
"sys_mod_count": "1",
"sys_updated_on": "2021-11-15 17:18:10",
"sys_tags": "",
"sys_class_name": "question_choice",
"published_ref": "",
"sys_id": "",
"sys_package": {
"link": "link goes here",
"value": "global"
},
"inactive": "false",
"sys_update_name": "question_choice_",
"sys_updated_by": "someone ",
"sys_created_on": "2021-11-15 17:17:35",
"sys_name": "BG Info",
"sys_scope": {
"link": "link goes here",
"value": "global"
},
"text": "BG Info",
"value": "BG Info",
"sys_created_by": "someone",
"misc": "0",
"order": "300",
"sys_policy": ""
},
{
"rec_misc": "0",
"question": {
"link": "link goes here",
"value": "5b42"
},
"sys_mod_count": "0",
"sys_updated_on": "2021-04-14 06:56:39",
"sys_tags": "",
"sys_class_name": "question_choice",
"published_ref": "",
"sys_id": "05c2d",
"sys_package": {
"link": "link goes here",
"value": "global"
},
"inactive": "false",
"sys_update_name": "question_choice_",
"sys_updated_by": "someone",
"sys_created_on": "2021-04-14 06:56:39",
"sys_name": "Baseline (Developer Plus Build Mgr)",
"sys_scope": {
"link": "link goes here",
"value": "global"
},
"text": "Baseline (Developer Plus Build Mgr)",
"value": "baseline",
"sys_created_by": "someone",
"misc": "0",
"order": "300",
"sys_policy": ""
},
使用
Wrapper results = JsonConvert.DeserializeObject<Wrapper>(result);
响应以数组形式出现,因此您必须正确映射数据。