Json Deserializer Returning Null Values

json deserialization returns null

You have an extra level of nesting {"cat": { /* CategoryVM contents */ }} that is not reflected in your data model. The easiest way to account for this is to deserialize to a wrapper object with a public CategoryVM cat property, which could be an anonymous type object:

var c = JsonConvert.DeserializeAnonymousType(cat, new { cat = default(CategoryVM) })
.cat.category;

Demo fiddle here.

Json Deserialize Object returns null when called

I fixed it and just in case somebody faced the same problem I'd like to post a solution on how I managed to solve it. Basically what I did was adding this class that appends additional settings onto a serializer:

 internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}

and then adding it as a parameter to the DeserializeObject method such as:

User user= JsonConvert.DeserializeObject<User>(json, Converter.Settings);

I hope it helps somebody.

JsonDeserialize is returning null values after attempting to bind to model

First, the name of your properties are not correct with json names; they should be same or you have to use a jsonProperty attribute. And the main class name is not correct; your main class is should be a class that includes your sub class like 'Payee', 'Address' and rest , please see my codes, i fixed a bit.

    public class PayInfo
{

public Payee Payee { get; set; }
public Payment Payment { get; set; }
public List<Remittance> Remittance { get; set; }

}
public class Payee
{
[JsonProperty("Name")]
public string PayeeName { get; set; }

[JsonProperty("Fax")]
public string PayeeFax { get; set; }

[JsonProperty("Phone")]
public string PayeePhone { get; set; }

public Address Address { get; set; }

public string Attention { get; set; }
public string SubmissionDate { get; set; }
public string PaymentExp { get; set; }
}


public class Payment
{
public string PAN { get; set; }
public string CVV { get; set; }
public string Exp { get; set; }


}
public class Address
{

public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string Zip { get; set; }

}
public class Remittance
{
public string PayorName { get; set; }
public string PayorId { get; set; }
public string InvoiceNo { get; set; }
public string Description { get; set; }
public string Amount { get; set; }
}

And you can modify your method like this as using list instead of array , will be easier.

     public static List<PayInfo> ReadJson()
{
// read file into a string and deserialize JSON to a type
var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonData\sample.json"));
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"JsonData\sample.json"))
{
JsonSerializer serializer = new JsonSerializer();
// PayInfo[] payInfoData = (PayInfo[])serializer.Deserialize(file, typeof(PayInfo[]));
payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
return payInfoData1;
}
}

C# JsonConvert.DeserializeObject returning null values for a json array of objects

When you do

JsonConvert.DeserializeObject<List<Model.AuxilioEmergencial>>(restResponse.Content);

You're telling the deserializer that you want a List, where every element is of type AuxilioEmergencial.

Your class AuxilioEmergencial has 1 property auxilio, which means every object in the JSON array has to have that property to be properly deserialized. E.g.:

[
{
"auxilio": {
"id ": 1,
"mesDisponibilizacao": "06/2020",
...
}
},
{
"auxilio": {
...
}
}
]

What you actually want is:

JsonConvert.DeserializeObject<List<Model.Auxilio>>(restResponse.Content);

Returning null value in JSON Deserialization

try this, it was tested in visual studio

var jsonParsed = JObject.Parse(json); 

List<EmployeeMasterData> employeeMasterData = jsonParsed["root"]["EmployeeMaster"]
["EmployeeMasterData"].Select(x => AddItems(x)).ToList();

public static EmployeeMasterData AddItems(JToken jt)
{
var emd = new EmployeeMasterData();
foreach (var element in ((JObject)jt).Properties())
{
if (element.Name == "BasicDetails")
emd.BasicDetails = element.Value.ToObject<BasicDetails>();
if (element.Name == "DependentDetails")
emd.DependentDetails = element.Value.ToObject<DependentDetails>();
if (element.Name == "ContactDetails")
emd.ContactDetails = element.Value.ToObject<ContactDetails>();
}
return emd;
}

classes

public class EmployeeMasterData
{

public BasicDetails BasicDetails { get; set; }

public DependentDetails DependentDetails { get; set; }

public ContactDetails ContactDetails { get; set; }
}

public class EmployeeMaster
{
public List<EmployeeMasterData> EmployeeMasterData { get; set; }
}

public class Root
{
public EmployeeMaster EmployeeMaster { get; set; }
}
public class BasicDetail
{
public string Action { get; set; }
public string EmployeeCode { get; set; }
public int? L2ManagerCode { get; set; }
public string Designation { get; set; }
public string SubDepartment { get; set; }
public string Department { get; set; }
public string JOBROLE { get; set; }
public string L2ManagerName { get; set; }
public string IsDisabled { get; set; }
public string L1ManagerCode { get; set; }
public string Gender { get; set; }
public string EmploymentType { get; set; }
public string L1ManagerName { get; set; }
public string FirstName { get; set; }
public string Title { get; set; }
public string EmploymentStatus { get; set; }
public string MiddleName { get; set; }
public string DisabilityType { get; set; }
public string OfficialMailID { get; set; }
public string Nationality { get; set; }
public string DateOfRelieving { get; set; }
public string DateOfJoining { get; set; }
public string LastName { get; set; }
public string BirthDate { get; set; }
}

public class BasicDetails
{
public BasicDetail BasicDetail { get; set; }
}

public class DependentDetail
{
public string DateOfBirth { get; set; }
public string DependentName { get; set; }
public string Action { get; set; }
public string EmployeeCode { get; set; }
public string Address { get; set; }
public string RelationshipType { get; set; }
public string Gender { get; set; }
public string IsDependent { get; set; }
}

public class DependentDetails
{
public DependentDetail DependentDetail { get; set; }
}

public class ContactDetail
{
public string AddressLine2 { get; set; }
public string AddressLine1 { get; set; }
public string Action { get; set; }
public string Country { get; set; }
public string City { get; set; }
public object Pincode { get; set; }
public string State { get; set; }
}

public class ContactDetails
{
public List<ContactDetail> ContactDetail { get; set; }
}




Related Topics



Leave a reply



Submit