Home > Back-end >  Json DeserializedObject how do I call the returned values
Json DeserializedObject how do I call the returned values

Time:05-26

How do I call the values from the Wrapper items. I assumed it would be for example items.SysName but no luck. I am calling a query that returns a list of text in json format that is then filtered with the jsonConvert I am just unable to call the results but I can view that they are there in the debugger. The end result is I will create a list of the SysName values and return said list.

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

Wrapper 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; }
    }

}

Image showing that results do show up.

Image showing that results do show up.

CodePudding user response:

items is a Wrapper which contains a collection of Result : items.Result.

You can iterate through it to access the results one by one. One way to do it is by using a foreach loop

foreach (Result result in items.Result)
{
    string sysName = result.SysName;
}

You can also use LinQ to retrieve all of the sysNames

IEnumerable<string> sysNames = items.Result.Select(result => result.SysName);

If you only care about the first result, just use

Result result = items.Result.First();
string sysName = result.SysName;

PS : Renaming Wrapper.Result to Wrapper.Results and var items = ... to var wrapper = ... is more explicit about your data.

  • Related