将 json 反序列化到我的 C# 对象中会出错
deserialize json into my C# object gives error
我得到这样的 JSON 并想将其反序列化到我的 C# 对象中。但这最终导致无法反序列化的错误。我可以获得解决此问题的帮助吗?
public class UserDto {
public string Id { get; set; }
public string Name { get; set; }
public List<string> Permissions { get; set; }
}
以上是绑定API输出的模型对象
HttpResponseMessage response = await client.GetAsync($"/users");
if (!response.IsSuccessStatusCode || response.Content == null) {
return null;
}
string result = await response.Content.ReadAsStringAsync();
UserDto users = await response.Content.ReadAsJsonAsync<UserDto>();
List<UserDto> x2 = JsonConvert.DeserializeObject<List<UserDto>>(result);
上述 getasync
方法给出了以下结果,我正在尝试将其反序列化为对象。
{
"users": [{
"id": "1",
"name": "ttest",
"permissions": [
"add",
"edit",
"delete"
]
}]
}
错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type 'System.Collections.Generic.List`1[****.Models.UserDto]'
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.
我得到这样的 JSON 并想将其反序列化到我的 C# 对象中。但这最终导致无法反序列化的错误。我可以获得解决此问题的帮助吗?
在您的 JSON 中,Users
JArray 在 JObject 中,因此要转换您的 JSON 字符串,您需要 List<User>
在 RootObject
class喜欢,
public class User
{
public string id { get; set; }
public string name { get; set; }
public List<string> permissions { get; set; }
}
public class Root
{
public List<User> users { get; set; }
}
反序列化,
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
我得到这样的 JSON 并想将其反序列化到我的 C# 对象中。但这最终导致无法反序列化的错误。我可以获得解决此问题的帮助吗?
public class UserDto {
public string Id { get; set; }
public string Name { get; set; }
public List<string> Permissions { get; set; }
}
以上是绑定API输出的模型对象
HttpResponseMessage response = await client.GetAsync($"/users");
if (!response.IsSuccessStatusCode || response.Content == null) {
return null;
}
string result = await response.Content.ReadAsStringAsync();
UserDto users = await response.Content.ReadAsJsonAsync<UserDto>();
List<UserDto> x2 = JsonConvert.DeserializeObject<List<UserDto>>(result);
上述 getasync
方法给出了以下结果,我正在尝试将其反序列化为对象。
{
"users": [{
"id": "1",
"name": "ttest",
"permissions": [
"add",
"edit",
"delete"
]
}]
}
错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type 'System.Collections.Generic.List`1[****.Models.UserDto]'
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.
我得到这样的 JSON 并想将其反序列化到我的 C# 对象中。但这最终导致无法反序列化的错误。我可以获得解决此问题的帮助吗?
在您的 JSON 中,Users
JArray 在 JObject 中,因此要转换您的 JSON 字符串,您需要 List<User>
在 RootObject
class喜欢,
public class User
{
public string id { get; set; }
public string name { get; set; }
public List<string> permissions { get; set; }
}
public class Root
{
public List<User> users { get; set; }
}
反序列化,
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);