locked
Task List issue RRS feed

  • Question

  • User-588203678 posted

    Hi,

    I have 

    public async Task<List<ValidationDataType>> GetValue(HtgValid htg)

    {

    List<ValidationDataType> items = new List<ValidationDataType>();

    HttpResponseMessage resp = await _HtgService.GetValue(htg);

    if(res.StatusCode ==HttpStatusCode.OK)

    {

    ///value return null

    //But in swaggger i am getting list of items

    items = JObject.Parse(response.Content.ReadAsStringAsync().Result.ToObject<List<ValidationDataType>>();

    item is null valu . instead it should return 

    [{

    "internalId":"saaddd",

    "internalCode":"dam"

    },

    }

    ValidationDataTypeClass has internalId and internalCode as get;set

    Saturday, May 1, 2021 3:26 PM

All replies

  • User-474980206 posted

    A list should be parsed as a JArray(), not an object

    Saturday, May 1, 2021 4:03 PM
  • User-588203678 posted

    How to do to get list of items using JArray() ?

    Saturday, May 1, 2021 4:21 PM
  • User-588203678 posted

    items = JArray.Parse(response.Content.ReadAsStringAsync().Result.ToObject<List<ValidationDataType>>();

    items is  0

    how to resolve

    Saturday, May 1, 2021 4:49 PM
  • User1120430333 posted
    public List<DtoAuthor> GetAll()
            {
                var dtoauthors = new List<DtoAuthor>();
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://localhost:8081/author/GetAll");
    
                    var response = client.GetAsync(uri).Result;
    
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
    
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
    
                    dynamic authors = JArray.Parse(responseString) as JArray;
    
                    foreach (var obj in authors)
                    {
                        DtoAuthor dto = obj.ToObject<DtoAuthor>();
    
                        dtoauthors.Add(dto);
                    }
                }
    
                return dtoauthors;
            }

    HTH

    Sunday, May 2, 2021 10:20 PM