C# s对指定Uri的GET请求,并在异步操作中以字符串形式返回响应正文 列表_deliveryItems=JsonConvert.DeserializeObject(内容)//将JSON字符串反序列化或转换为Post的集合

C# s对指定Uri的GET请求,并在异步操作中以字符串形式返回响应正文 列表_deliveryItems=JsonConvert.DeserializeObject(内容)//将JSON字符串反序列化或转换为Post的集合,c#,json.net,C#,Json.net,下面标记的答案确实是正确的,我能够通过调整我的方法获得数据,如下所示 public DeliveryDataStore() { Task<string> callTask = Task.Run(() => GetDeliverysFromAPi()); // Wait for it to finish callTask.Wait(); // Get the result string conten

下面标记的答案确实是正确的,我能够通过调整我的方法获得数据,如下所示

public DeliveryDataStore()
{

        Task<string> callTask = Task.Run(() => GetDeliverysFromAPi());
        // Wait for it to finish
        callTask.Wait();
        // Get the result
        string content = callTask.Result;

        //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation
         deliverysItems = JsonConvert.DeserializeObject<List<DeliverysItems>>(content); //Deserializes or converts JSON String into a collection of Post


 }
public DeliveryDataStore()
{
Task callTask=Task.Run(()=>GetDeliverysFromAPi());
//等它结束
callTask.Wait();
//得到结果
字符串内容=callTask.Result;
//向指定的Uri发送GET请求,并在异步操作中将响应正文作为字符串返回
deliverysItems=JsonConvert.DeserializeObject(content);//反序列化JSON字符串或将其转换为Post的集合
}

问题在于,您的方法
GetDeliverysFromAPi()
返回一个
任务

但是,
Task
不会覆盖
ToString()
,因此它只会返回完整的类型名
“System.Threading.Tasks.Task`1[System.String]”
(demo fiddle#1)

相反,请使用以获得结果:

string content = GetDeliverysFromAPi().Result;
注意文档中的
结果

访问属性的get访问器会阻塞调用线程,直到异步操作完成;这相当于调用Wait方法

操作的结果可用后,将存储该结果,并在后续调用result属性时立即返回该结果

工作演示小提琴#2

顺便说一句,在你编辑的问题中

string content = GetDeliverysFromAPi(); //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation

这甚至不会编译,因此可能是问题中的输入错误;请参阅demo fiddle#3。

我相信这是因为嵌套对象中存在一些循环依赖关系。调用api时数据显示不正常,所以当我在应用程序中时怎么会这样。更改模型结构后,我能够反序列化JSON。示例:@RahulSharma,但我不希望它被称为RootOjbect,这对另一个开发人员来说是什么意思?还有,为什么需要将int更改为objects@dbc这里的to字符串是I livant,但是问题已经做了调整谢谢你的帮助我希望反对票消失,这确实是问题所在,请看我上面的编辑,我将其包装在一个任务运行方法中,并进行了处理thanks@rogue39nin-投票不是我投的。但是在最近的编辑中,您现在正在另一个任务中包装
GetDeliverysFromAPi
task.Run(()=>GetDeliverysFromAPi())我不确定在另一个任务中包装一个任务会得到什么,但似乎没有必要。我认为这是因为我似乎无法将该类标记为async类不是异步的,方法是异步的。看见
string content = GetDeliverysFromAPi(); //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation
List<DeliverysItems> _deliveryItems =JsonConvert.DeserializeObject<List<DeliverysItems>>(content); //Deserializes or converts JSON String into a collection of Post
public DeliveryDataStore()
{

        Task<string> callTask = Task.Run(() => GetDeliverysFromAPi());
        // Wait for it to finish
        callTask.Wait();
        // Get the result
        string content = callTask.Result;

        //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation
         deliverysItems = JsonConvert.DeserializeObject<List<DeliverysItems>>(content); //Deserializes or converts JSON String into a collection of Post


 }
public async Task<String> GetDeliverysFromAPi() { /* Contents omitted */ }
string content = GetDeliverysFromAPi().ToString();
string content = GetDeliverysFromAPi().Result;
string content = GetDeliverysFromAPi(); //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation