Search code examples
c#jsonjson-deserialization

Returning JSON to OutputBuffer


I am having issues when returning a JSON string back to an output after I deserialized it.

I have the following three classes declared that I generated from json2csharp:

public class Application
{
    public int App_ID { get; set; }
    public string App_Ref { get; set; }
    public string Status { get; set; }
    public string Error_Code { get; set; }
    public string Error_Message { get; set; }
    public string Create_Dt { get; set; }
    public string Modify_Dt { get; set; }
    public string Client_Name { get; set; }
    public string Client_Code { get; set; }
    public string Centrelink_Status { get; set; }

}
public class Response
{
    public List<Application> Applications { get; set; }
    public string Current_Dt { get; set; }
    public string Last_App_Dt { get; set; }
    public int Count { get; set; }
    public int Total { get; set; }
}

public class RootObject
{
    public bool Success { get; set; }
    public Response jsonResponse { get; set; }

}

My JSON Response looks like this:

    {
      "Success": true,
      "Response": {
        "Applications": [
          {
            "App_ID": 2877582,
            "App_Ref": "Odonnell",
            "Status": "Complete",
            "Error_Code": 0,
            "Error_Message": "",
            "Create_Dt": "2016-10-09 19:28:18.867 +00:00",
            "Modify_Dt": "2016-10-09 19:33:10.810 +00:00",
            "Client_Name": "Aussie Bike Auto & Boat Loans South",
            "Client_Code": "GGT31",
            "Centrelink_Status": "Receiving_Logons"
          },
          {
            "App_ID": 2878070,
            "App_Ref": "alliso",
            "Status": "Quicklink",
            "Error_Code": null,
            "Error_Message": null,
            "Create_Dt": "2016-10-09 21:55:49.220 +00:00",
            "Modify_Dt": "2016-10-09 21:55:49.220 +00:00",
            "Client_Name": "KChristoforidis",
            "Client_Code": "GGT05",
            "Centrelink_Status": "Receiving_Logons"
          }...
    ],
        "Current_Dt": "2016-11-13 22:52:41.581 +00:00",
        "Last_App_Dt": "2016-10-11 01:42:25.470 +00:00",
        "Count": 65,
        "Total": 65
      }
}

I am able to output the "Success" bool from the Response but I am unable to get back anything else from the response as I am getting a "Object reference not set to an instance of an object." error when I try to do the following:

foreach (Application app in outPutResponse.jsonResponse.Applications)
{
    ApplicationBuffer.AddRow();
    ApplicationBuffer.AppID = app.App_ID;
    ApplicationBuffer.AppRef = app.App_Ref;
    ApplicationBuffer.Status = app.Status;

}

I also can't return the Response "jsonResponse" to the outputBuffer with an error "can not implicitly convert jsonResponse to BlobColumn"

I also can't return the Response value from outPutResponse.jsonResponse with the following errors:

    RawBuffer.AddRow();
    RawBuffer.ResponseSuccess = outPutResponse.Success;
    RawBuffer.Response.AddBlobData(Encoding.ASCII.GetBytes(outPutResponse.jsonResponse));

The best overloaded method match for 'System.Text.Encoding.GetBytes(char[])' has some invalid arguments Argument 1: cannot convert from 'Script.Response' to 'char[]'

This is my final stumbling block and I can't seem to get this right.


Solution

  • Assuming you are using Newtonsoft JSON.Net since that is a smart idea.

    You have changed the property name to jsonResponse when the actual property name is Response.

    If you want to changes the names as you like you have to decorate the properties with

    [JsonProperty("myproperty_name")] 
    

    like so:

    public class Application
    {
        [JsonProperty("App_ID")]
        public int App_ID { get; set; }
    
        [JsonProperty("App_Ref")]
        public string App_Ref { get; set; }
    
        [JsonProperty("Status")]
        public string Status { get; set; }
    
        [JsonProperty("Error_Code")]
        public int? Error_Code { get; set; }
    
        [JsonProperty("Error_Message")]
        public string Error_Message { get; set; }
    
        [JsonProperty("Create_Dt")]
        public string Create_Dt { get; set; }
    
        [JsonProperty("Modify_Dt")]
        public string Modify_Dt { get; set; }
    
        [JsonProperty("Client_Name")]
        public string Client_Name { get; set; }
    
        [JsonProperty("Client_Code")]
        public string Client_Code { get; set; }
    
        [JsonProperty("Centrelink_Status")]
        public string Centrelink_Status { get; set; }
    }
    
    public class Response
    {
        [JsonProperty("Applications")]
        public IList<Application> Applications { get; set; }
    
        [JsonProperty("Current_Dt")]
        public string Current_Dt { get; set; }
    
        [JsonProperty("Last_App_Dt")]
        public string Last_App_Dt { get; set; }
    
        [JsonProperty("Count")]
        public int Count { get; set; }
    
        [JsonProperty("Total")]
        public int Total { get; set; }
    }
    
    public class RootObject
    {
        [JsonProperty("Success")]
        public bool Success { get; set; }
    
        [JsonProperty("Response")]
        public Response jsonResponse { get; set; }
    }
    

    While I recommend the above example you could also use Response instead ofc:

    public Response Response { get; set; }