Hello Guys Please i am having issues getting the elements in data into a list.. I want to be able to get User_ID
, Country
, Continent
and other elements to a list after which i will do a bulk insert to the database.
我得到的错误 错误信息 mscorlib.dll中发生类型为'Newtonsoft.Json.JsonSerializationException'的异常,但未在用户代码中处理
附加信息:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [Country_API.Response]',因为该类型需要JSON数组(例如[1 ,2,3])正确反序列化。
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.
这是从API返回的JSon数据
{
"Status": "200 Ok",
"Message": "Data retrieved",
"Response": {
"current_page": 1,
"data": [
{
"User_ID": "EAD001",
"Country": "Ghana",
"Continent": "Africa",
"Gender": "Male",
"Email": "ead1@yahoo.com",
"Religion": ""
},
{
"User_ID": "EAD002",
"Country": "Senegal",
"Continent": "Africa",
"Gender": "Female",
"Email": "ead2@yahoo.com",
"Religion": "Muslim"
}
]
}
}
我正在尝试反序列化,但会引发以上错误..这就是我正在尝试的
if (result.IsSuccessStatusCode)
{
string toJsonString = await result.Content.ReadAsStringAsync();
var deserialize = JsonConvert.DeserializeObject<List<Response>>(toJsonString);
}
杰森模型
public class Data
{
public string User_ID { get; set; }
public string Country { get; set; }
public string Continent { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public string Religion { get; set; }
}
public class Response
{
public int current_page { get; set; }
public IList<Data> data { get; set; }
}
public class Application
{
public string Status { get; set; }
public string Message { get; set; }
public Response Response { get; set; }
}
我该如何实现呢?
您正在尝试反序列化对象内部的List。您需要反序列化整个对象。尝试这个: