反序列化 Json 个对象的列表只给我一个对象名称的字符串?
Deserilizing a list of Json Objects only gives me a string of the object name?
我有两个项目。一个正在向另一个发送 GET 请求以获得列表,该列表等同于发送请求的应用程序的列表。
调试时,我的 responseString 等于“System.Collections.Generic.List'1”,并且在 JSON 可视化工具中显示字符串不是 JSON 格式。为什么我的回复没有被传递为 JSON?
我知道 .NET 有一些方法可以让事情变得更简单,但我的项目卡在 .NET Framework 4.6.1 上
此代码发送 GET
[HttpGet]
public async Task<bool> GetUnsentApplicationsFromHiring()
{
try
{
using (var client = new HttpClient())
{
var uri = new Uri("http://localhost:2904/Home/SendIntermediateApplicationsToDataNet");
var response = await client.GetAsync(uri);
var responseString = await response.Content.ReadAsStringAsync();
var apps = JsonConvert.DeserializeObject<List<EmployeeApplication>>(responseString);
return true;
}
}
这是接收 GET 并发回列表的代码。
[HttpGet]
public async Task<List<JobApplication>> SendIntermediateApplicationsToDataNet()
{
try
{
var apps = DB<JobApplication>().GetAll().Where(x => x.CompletionStatus == CompletionStatusFlag.CompletedStep1
|| x.CompletionStatus == CompletionStatusFlag.CompletedStep2).ToList();
foreach(var app in apps)
{
if (app.CompletionStatus == CompletionStatusFlag.CompletedStep1)
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep1;
}
else
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep2;
}
}
return apps;
}
请将 return 函数更改为:
[HttpGet]
public async Task<string> SendIntermediateApplicationsToDataNet()
{
try
{
var apps = DB<JobApplication>().GetAll().Where(x => x.CompletionStatus == CompletionStatusFlag.CompletedStep1
|| x.CompletionStatus == CompletionStatusFlag.CompletedStep2).ToList();
foreach(var app in apps)
{
if (app.CompletionStatus == CompletionStatusFlag.CompletedStep1)
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep1;
}
else
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep2;
}
}
return JsonConvert.SerializeObject(apps);;
}
我有两个项目。一个正在向另一个发送 GET 请求以获得列表,该列表等同于发送请求的应用程序的列表。
调试时,我的 responseString 等于“System.Collections.Generic.List'1”,并且在 JSON 可视化工具中显示字符串不是 JSON 格式。为什么我的回复没有被传递为 JSON?
我知道 .NET 有一些方法可以让事情变得更简单,但我的项目卡在 .NET Framework 4.6.1 上
此代码发送 GET
[HttpGet]
public async Task<bool> GetUnsentApplicationsFromHiring()
{
try
{
using (var client = new HttpClient())
{
var uri = new Uri("http://localhost:2904/Home/SendIntermediateApplicationsToDataNet");
var response = await client.GetAsync(uri);
var responseString = await response.Content.ReadAsStringAsync();
var apps = JsonConvert.DeserializeObject<List<EmployeeApplication>>(responseString);
return true;
}
}
这是接收 GET 并发回列表的代码。
[HttpGet]
public async Task<List<JobApplication>> SendIntermediateApplicationsToDataNet()
{
try
{
var apps = DB<JobApplication>().GetAll().Where(x => x.CompletionStatus == CompletionStatusFlag.CompletedStep1
|| x.CompletionStatus == CompletionStatusFlag.CompletedStep2).ToList();
foreach(var app in apps)
{
if (app.CompletionStatus == CompletionStatusFlag.CompletedStep1)
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep1;
}
else
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep2;
}
}
return apps;
}
请将 return 函数更改为:
[HttpGet]
public async Task<string> SendIntermediateApplicationsToDataNet()
{
try
{
var apps = DB<JobApplication>().GetAll().Where(x => x.CompletionStatus == CompletionStatusFlag.CompletedStep1
|| x.CompletionStatus == CompletionStatusFlag.CompletedStep2).ToList();
foreach(var app in apps)
{
if (app.CompletionStatus == CompletionStatusFlag.CompletedStep1)
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep1;
}
else
{
app.SentCompletionStatus = SentCompletionStatusFlag.SentStep2;
}
}
return JsonConvert.SerializeObject(apps);;
}