将JSON返回到OutputBuffer

时间:2016-11-14 00:54:28

标签: c# json api web ssis

我遇到问题在我对其进行反序列化之后将JSON字符串返回到输出。

我声明了从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; }

    }

我的JSON响应看起来像这样。

    {
      "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
      }
}

我能够输出&#34;成功&#34;响应中的bool但我无法从响应中找回任何其他内容,因为我得到的&#34;对象引用未设置为对象的实例。&#34; 错误,当我尝试并执行以下操作。

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

       }

我也无法返回响应&#34; jsonResponse&#34;使用&#34;到outputBuffer,不能将jsonResponse隐式转换为BlobColumn&#34;

我也无法从outPutResponse.jsonResponse返回Response值,但有以下错误。

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

最佳重载方法匹配&#39; System.Text.Encoding.GetBytes(char [])&#39;有一些无效的论点    参数1:无法转换为&#39; Script.Response&#39;到&#39; char []&#39;

这是我最后的绊脚石,似乎无法做到这一点。有人可以帮忙吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

假设您正在使用Newtonsoft JSON.Net,因为这是一个明智的想法。

当实际属性名称为jsonResponse时,您已将属性名称更改为Response

如果您想根据需要更改名称,则必须使用

修饰属性
[JsonProperty("myproperty_name")] 
像这样:

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; }
}

虽然我推荐上面的示例,但您也可以使用Response代替c:

public Response Response { get; set; }