繁体   English   中英

过滤来自 API 调用的 JSON 响应

[英]Filter JSON response from an API call

基本上我正在做一个 api 调用,我收到 JSON 格式的 api 调用,我需要基本上过滤 api 调用,只返回 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;
            
        }

然后我有一个使用 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; }
    }

}

显示项目出现空

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

响应以数组形式出现,因此您必须正确映射数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM