Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t使用System.Text.JSON.JsonSerializer类反序列化我的JSON响应_C#_Json_Dotnet Httpclient - Fatal编程技术网

C# Can';t使用System.Text.JSON.JsonSerializer类反序列化我的JSON响应

C# Can';t使用System.Text.JSON.JsonSerializer类反序列化我的JSON响应,c#,json,dotnet-httpclient,C#,Json,Dotnet Httpclient,我正在使用System.Text.Json对我的Json进行反序列化,我知道Json.Net并选择不使用它。 我正在使用返回字符串的System.Net.Http.HttpClient.GetStringAsync方法请求。对请求的响应是一个JSON数组,其中填充了具有以下键的对象:userId、id、title和body。我正在使用反序列化通用方法反序列化我的JSON,其中Posts是我的返回类型,response是我的字符串 通过捕获异常,我得到如下所示的错误 我目前有 static rea

我正在使用
System.Text.Json
对我的
Json
进行反序列化,我知道
Json.Net
并选择不使用它。

我正在使用返回字符串的
System.Net.Http.HttpClient.GetStringAsync
方法请求。对请求的响应是一个
JSON
数组,其中填充了具有以下键的对象:
userId
id
title
body
。我正在使用
反序列化
通用
方法反序列化我的
JSON
,其中
Posts
是我的返回类型,
response
是我的字符串

通过捕获异常,我得到如下所示的错误

我目前有

static readonly HttpClient=new HttpClient();
静态异步任务主(字符串[]args)
{
等待GetPostAsync();
}
静态异步任务GetPostAsync()
{
client.BaseAddress=新Uri(“https://jsonplaceholder.typicode.com/");
字符串location=“posts”;
尝试
{
string response=wait client.GetStringAsync(位置);
Posts deserializedPosts=JsonSerializer.Deserialize(响应);
Console.WriteLine(反序列化邮件);
}
捕获(System.e例外)
{
控制台写入线(e);
}
}
班级职务
{
public int userId{get;set;}
公共int id{get;set;}
公共字符串标题{get;set;}
公共字符串体{get;set;}
}
我收到的错误

System.Text.Json.JsonException: The JSON value could not be converted to fugo.Program+Posts. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   at System.Text.Json.JsonSerializer.HandleStartArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at fugo.Program.GetPostAsync() in /home/kepler/PROJECTS/C-SHARP/fugo/Program.cs:line 27
更改此行:

Posts deserializedPosts = JsonSerializer.Deserialize<Posts>(response);
Posts deserializedPosts=JsonSerializer.Deserialize(响应);
为此:

var deserializedPosts = JsonSerializer.Deserialize<List<Posts>>(response);
var deserializedPosts=JsonSerializer.Deserialize(响应);

返回的数据是一个数组,而不是一个对象。

也许您可以发布一个负载示例。很多时候,类似这样的问题是由双重序列化或无效字符或其组合引起的。你能在网上发布什么吗?所谓有效载荷,你是指回应吗?@RossBush——它在代码中:
https://jsonplaceholder.typicode.com/posts