I want to save my json code to mongoDB. But I dont save it to mongoDB. I'm getting an error.I searched a lot but couldn't figure it out.
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[KitapjetFormApp.Alias]' 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) 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. Path 'User[1].Documents.Document.Alias.Name', line 43, position 19.'
Myjson Code is
{
"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"
}
}
}
}
]
}
Myclass is
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; }
}
My code is here for mongodb insert
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);
}