0

I am using the following class -

    public class RequestObject
{
    public string loanPurpose { get; set; }
    public string occupancy { get; set; }
    public string propertyType { get; set; }
    public string condoStyle { get; set; }
    public int purchasePrice { get; set; }
    public string propertyValue { get; set; }
    public double loanAmount { get; set; }
    public int loanAmountPCT { get; set; }
    public string secondLien { get; set; }
    public string maxLimit { get; set; }
    public string curBalance { get; set; }
    public string disposition { get; set; }
    public string openedDate { get; set; }
    public int zipcode { get; set; }
    public string creditScore { get; set; }
    public string loanProgram { get; set; }
    public string pleaseSpecify { get; set; }
    public string orgPurchasePrice { get; set; }
    public string cmYearStart { get; set; }
    public string escrowAccount { get; set; }
    public string unitsNo { get; set; }
    public string lockPeriod { get; set; }
    public string payoffAmount { get; set; }
    public string f1OptStatus { get; set; }
}

I am building an object from the class like so -

            var jsonObject = new RequestObject
        {
            loanPurpose = "Purchase",
            occupancy = "Primary Residence",
            propertyType = "Townhouse",
            condoStyle = "Attached",
            purchasePrice = purchasePrice,
            propertyValue = null,
            loanAmount = actualLoanAmount,
            loanAmountPCT = loanPercent,
            secondLien = null,
            maxLimit = null,
            curBalance = null,
            disposition = null,
            openedDate = null,
            zipcode = zipCode,
            creditScore = "780 +",
            loanProgram = "30 Year Fixed",
            pleaseSpecify = null,
            orgPurchasePrice = null,
            cmYearStart = null,
            escrowAccount = "With Escrow Account",
            unitsNo = null,
            lockPeriod = "30 Days",
            payoffAmount = null,
            f1OptStatus = "No"
        };

I am then calling the Serialize method -

            var json = request.JsonSerializer.Serialize(jsonObject);

When I execute my application, I get 'Object reference not set to an instance of an object' on the Serialize method line.

I thought I am building the object correctly. What is going wrong?

Thank you!

Michael
  • 23
  • 6

1 Answers1

2

I would hazard a guess that your request object doesn't have a JSONSerializer.

If you trying to serialize the object to a string to return, you can use the following line of code instead.

var json = JsonConvert.Serialize(jsonObject);

This will serialize your object, into JSON.

If you want to deserialize, you can use the deserialize method.

var deserializedObject = JsonConvert.Deserialize(json)
Malcor
  • 2,667
  • 21
  • 29
  • Hello Malcor. I am trying to serialize it to be used for a Rest API call, hence the 'request' object. I am using the RestSharp class library. I do not see a JsonConvert method available in that library. – Michael Jan 13 '20 at 14:26
  • Maybe try `JsonSerializer.Serialize(jsonObject)` and not on the request. – Malcor Jan 13 '20 at 14:29
  • Okay, I think this can be considered the answer. I added Newtonsoft.Json to my project and used the Serialize method on the JsonConvert class and then used RestSharp for the request to call the API and I was able to get a response. Thanks very much! I am surprised to have read an example that used the Serialize I posted above when it clearly does not work that way. – Michael Jan 13 '20 at 14:34