[英]Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly
I have this JSON:我有这个 JSON:
[
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 1",
"Values": [
"Acc 1"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "1",
"Values": [
"1"
]
}
}
],
"Name": "account",
"Id": "1"
},
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 2",
"Values": [
"Acc 2"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "2",
"Values": [
"2"
]
}
}
],
"Name": "account",
"Id": "2"
},
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 3",
"Values": [
"Acc 3"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "3",
"Values": [
"3"
]
}
}
],
"Name": "account",
"Id": "2"
}
]
And I have these classes:我有这些课程:
public class RetrieveMultipleResponse
{
public List<Attribute> Attributes { get; set; }
public string Name { get; set; }
public string Id { get; set; }
}
public class Value
{
[JsonProperty("Value")]
public string value { get; set; }
public List<string> Values { get; set; }
}
public class Attribute
{
public string Key { get; set; }
public Value Value { get; set; }
}
I am trying to deserialize the above JSON using the code below:我正在尝试使用以下代码反序列化上述 JSON:
var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>(JsonStr);
but I am getting this error:但我收到此错误:
Cannot deserialize the current JSON array (eg [1,2,3]) into type 'test.Model.RetrieveMultipleResponse' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“test.Model.RetrieveMultipleResponse”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。 To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array.
要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
JsonArrayAttribute 也可以添加到类型以强制它从 JSON 数组反序列化。 Path '', line 1, position 1.
路径 '',第 1 行,位置 1。
Your json string is wrapped within square brackets ( []
), hence it is interpreted as array instead of single RetrieveMultipleResponse
object.您的 json 字符串包含在方括号 (
[]
) 中,因此它被解释为数组而不是单个RetrieveMultipleResponse
对象。 Therefore, you need to deserialize it to type collection of RetrieveMultipleResponse
, for example :因此,您需要将其反序列化为
RetrieveMultipleResponse
类型集合,例如:
var objResponse1 =
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
If one wants to support Generics (in an extension method) this is the pattern...如果有人想支持泛型(在扩展方法中),这就是模式......
public static List<T> Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<List<T>>(SerializedJSONString);
return stuff;
}
It is used like this:它是这样使用的:
var rc = new MyHttpClient(URL);
//This response is the JSON Array (see posts above)
var response = rc.SendRequest();
var data = response.Deserialize<MyClassType>();
MyClassType looks like this (must match name value pairs of JSON array) MyClassType 看起来像这样(必须匹配 JSON 数组的名称值对)
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class MyClassType
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "Manager")]
public string Manager { get; set; }
[JsonProperty(PropertyName = "LastUpdate")]
public DateTime LastUpdate { get; set; }
}
Use NUGET to download Newtonsoft.Json add a reference where needed...使用 NUGET 下载 Newtonsoft.Json 在需要的地方添加引用...
using Newtonsoft.Json;
Can't add a comment to the solution but that didn't work for me.无法向解决方案添加评论,但这对我不起作用。 The solution that worked for me was to use:
对我有用的解决方案是使用:
var des = (MyClass)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(MyClass)); return des.data.Count.ToString();
Deserializing JSON array into strongly typed .NET object 将 JSON 数组反序列化为强类型 .NET 对象
Use this, FrontData
is JSON string:使用这个,
FrontData
是 JSON 字符串:
var objResponse1 = JsonConvert.DeserializeObject<List<DataTransfer>>(FrontData);
and extract list:并提取列表:
var a = objResponse1[0];
var b = a.CustomerData;
To extract the first element (Key) try this method and it will be the same for the others :要提取第一个元素(键),请尝试使用此方法,其他元素也是如此:
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("Your URL"))
{
var apiResponse = await response.Content.ReadAsStringAsync();
var list = JObject.Parse(apiResponse)["Attributes"].Select(el => new { Key= (string)el["Key"] }).ToList();
var Keys= list.Select(p => p.Key).ToList();
}
}
var objResponse1 =
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
worked!工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.