![](/img/trans.png)
[英]Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object
[英]Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"})
我想將我的 json 代碼保存到 mongoDB。 但我沒有將它保存到 mongoDB。 我收到一個錯誤。我搜索了很多但無法弄清楚。
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.IList`1[KitapjetFormApp.Alias]' because the type requires a JSON array (例如 [1,2,3])正確反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an數組或列表)可以從 JSON object 反序列化。 JsonObjectAttribute 也可以添加到類型中以強制它從 JSON object 反序列化。 路徑“用戶 [1].Documents.Document.Alias.Name”,第 43 行,position 19。
Myjson 代碼是
{
"User": [
{
"Identifier": "317933170",
"Title": "--AT ŞA--N",
"Type": "OZEL",
"FirstCreationTime": "2021-03-11T19:14:53",
"AccountType": "OZELENTEGRASYON",
"Documents": {
"Document": {
"@type": "Invoice",
"Alias": [
{
"Name": "urn:mail:gpk@g",
"CreationTime": "2021-03-09T15:36:56"
},
{
"Name": "urn:mail:hpk@h",
"CreationTime": "2021-03-11T19:12:53"
},
{
"Name": "urn:mail:rtypk@rty",
"CreationTime": "2021-03-11T18:25:55"
},
{
"Name": "urn:mail:qwspk@wqwq",
"CreationTime": "2021-03-11T19:14:53"
}
]
}
}
},
{
"Identifier": "74102",
"Title": "GID TAR ÜRÜN İNŞ YAPI MALZ HAY MADENCİLİK TAAH SAN VE TİC LTD ŞTİ",
"Type": "OZEL",
"FirstCreationTime": "2022-07-02T13:23:23",
"AccountType": "GIBPORTAL",
"Documents": {
"Document": {
"@type": "Invoice",
"Alias": {
"Name": "urn:mail:defaultpk@hotmail.com",
"CreationTime": "2022-07-02T13:23:23"
}
}
}
},
{
"Identifier": "83303831",
"Title": "HAFRİYAT İNŞAAT SANAYİ VE TİCARET LİMİTED ŞİRKETİ",
"Type": "OZEL",
"FirstCreationTime": "2022-07-01T12:27:14",
"AccountType": "GIBPORTAL",
"Documents": {
"Document": {
"@type": "Invoice",
"Alias": {
"Name": "urn:mail:defaultpk@hotmail.com",
"CreationTime": "2022-07-01T12:27:14"
}
}
}
},
{
"Identifier": "0860986",
"Title": "A YAPI MALZEMELER¦ VE ¦NÌAAT SANAY¦ T¦CARET L¦M¦TED ̦RKET¦",
"Type": "OZEL",
"FirstCreationTime": "2021-06-25T18:03:06",
"AccountType": "GIBPORTAL",
"Documents": {
"Document": {
"@type": "Invoice",
"Alias": {
"Name": "urn:mail:defaultpk@gmail.com",
"CreationTime": "2021-06-25T18:03:06"
}
}
}
}
]
}
我的班級是
public class Alias
{
public string Name { get; set; }
public DateTime CreationTime { get; set; }
}
public class Document
{
public string @type { get; set; }
public IList<Alias> Alias { get; set; }
}
public class Documents
{
public Document Document { get; set; }
}
public class User
{
public string Identifier { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public DateTime FirstCreationTime { get; set; }
public string AccountType { get; set; }
public Documents Documents { get; set; }
}
public class MukellefRoot
{
public IList<User> User { get; set; }
}
我的代碼是 mongodb 插件
using (StreamReader file = File.OpenText(@"C:\mukellef\mukellef.json"))
{
JsonSerializer serializer = new JsonSerializer();
MukellefRoot mukellefler = (MukellefRoot)serializer.Deserialize(file, typeof(MukellefRoot));
var mukellef = mukellefler.User.ToList();
mukellefCollectionItems.InsertMany(mukellef);
}
我找到了解決方案
using Newtonsoft.Json;
dynamic results = JsonConvert.DeserializeObject<dynamic>(YOUR_JSON);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.