Search code examples
c#asp.netwinformshttpclientdotnet-httpclient

HttpClient.GetStringAsync is returning a "Result" object?


I am trying to retrieve a JSON list from my own API with the GetStringAsync method. when I get it it returns as a "Result" object instead of just a string ?

Then I am trying to Deserialize the JSON array to a list but recieve an error .

This is how the returned string in the DEBUGGER looks from the HttpCLient.GetStringAsync :

"{\"Result\":[{\"id\":92,\"name\":\"Chris Hemsworth\",\"birthDate\":\"1983-8-11\",\"role\":\"Producer\",\"originalList\":null},{\"id\":90,\"name\":\"Jennifer Aniston\",\"birthDate\":\"1969-2-11\",\"role\":\"Producer\",\"originalList\":null},{\"id\":40,\"name\":\"Edward Norton\",\"birthDate\":\"1969-8-18\",\"role\":\"Writer\",\"originalList\":null}],\"Id\":71,\"Exception\":null,\"Status\":5,\"IsCanceled\":false,\"IsCompleted\":true,\"CreationOptions\":0,\"AsyncState\":null,\"IsFaulted\":false}"

the Exception im getting when trying to convert the JSON object to string:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BuisnessLogic.Actor]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.Path 'Result', line 1, position 10.'

UPDATE:

Here is the code:

var celebrities = JsonConvert.DeserializeObject<List<Actor>>(await client.GetStringAsync($"{serverAddress}/values/{GET_CELEBS_COMMAND}"));

the Actor class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuisnessLogic
{
    public class Actor
    {
        public int id { get; set; }
        public string name { get; set; }
        public string birthDate { get; set; }
        public string role { get; set; }
        public List<Actor> originalList { get; set; }

        public Actor(string name, string birthDate, string role)
        {
            this.name = name;
            this.birthDate = birthDate;
            this.role = role;
        }

        public Actor()
        {

        }

        public override string ToString()
        {
            return "Person: " + id + "" + name + " " + birthDate + " " + role;
        }

    }
}

EDIT 2 :

The Controller:

  using BuisnessLogic;

using System.Web.Mvc;

namespace WebApplication12.Controllers
{
    public class ValuesController : Controller
    {
        public ILogic _Ilogic;
        public ValuesController(ILogic logic)
        {
            _Ilogic = logic;

        }

        // GET api/values

        public ActionResult GetActors()
        {
            return Json(_Ilogic.GetAllActorsAsync(), JsonRequestBehavior.AllowGet);
        }

    }
}

The data management class :

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Configuration;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Linq;


namespace BuisnessLogic
{
    public class Logic : ILogic
    {
        static string filePath;
        private static ConcurrentDictionary<string, Actor> originalList;
        const string BACKUP = @"D:\backup.txt";

        static Logic()
        {
            originalList = new ConcurrentDictionary<string, Actor>();
            filePath = ConfigurationManager.AppSettings["tempList"];
            File.Copy(filePath, BACKUP, true);
            SaveOriginal();
        }



        public async static Task<List<Actor>> GetCelebritiesInner()
        {
            return originalList.Values.ToList();
        }

        public async Task<List<Actor>> GetAllActorsAsync()
        {
            return await GetCelebritiesInner();
        }




        // Try to read the data from the Json and initialize it. if failed , initialize with whatever it got. return 
        private static List<Actor> ReadActorsFromJson(string json)
        {
            List<Actor> celebListReadFromFile;
            try
            {

                var celebJson = File.ReadAllText(json);
                celebListReadFromFile = JsonConvert.DeserializeObject<List<Actor>>(celebJson);

            }
            catch (Exception ex)
            {
                celebListReadFromFile = new List<Actor>();
                // Empty list/whatever it got in it
            }
            return celebListReadFromFile;
        }

        public async Task RemoveActorAsync(string name)
        {

            if (originalList.TryRemove(name, out Actor removedActor))
            {
                var jsonToWrite = JsonConvert.SerializeObject(await GetCelebritiesInner());

                try
                {

                    File.WriteAllText(filePath, jsonToWrite);
                }
                catch (Exception ex)
                {
                    //Unable to remove due to an error.

                }
            }
        }

        public async Task ResetAsync()
        {

            UpdateFile();
        }

        //Saving the actor, adding the name as key & object as value.
        public static void SaveOriginal()
        {
            foreach (var currCeleb in ReadActorsFromJson(filePath))
            {
                originalList.TryAdd(currCeleb.name, currCeleb);
            }
        }

        public static void UpdateFile()
        {
            File.WriteAllText(filePath, string.Empty);
            var text = File.ReadAllText(BACKUP);
            File.WriteAllText(filePath, text);
        }
    }
}

The update method that goes to the uri and gets the string :

public async void update()
    {
        var b = await client.GetStringAsync($"{serverAddress}/values/{GET_CELEBS_COMMAND}");

        var celebrities = JsonConvert.DeserializeObject<List<Actor>>(b);
        foreach (Actor actor in celebrities)
        {
            actorBindingSource.Add(actor);
        }
    }

Solution

  • The JSON shown in your question is a serialised instance of Task<T>, which includes properties such as Result, AsyncState and IsFaulted. It's clear that this should be a serialised instance of T (List<Actor> in your case), which usually means there's a missing await somewhere.

    This "missing await" is in your ValuesController.GetActors, which is passing the result of ILogic.GetAllActorsAsync into Json. This ends up with an instance of Task<List<Actor>> being passed in, instead of just List<Actor>. In order to resolve that, use await, like this:

    public async Task<ActionResult> GetActors()
    {
        return Json(await _Ilogic.GetAllActorsAsync(), JsonRequestBehavior.AllowGet);
    }
    

    This also requires making GetActors async, as I've shown above.