2016-11-14 76 views
0

我遇到問題將JSON字符串反序列化後返回到輸出。將JSON返回給OutputBuffer

我有以下三個類聲明,我從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 
     } 
} 

我能夠輸出響應「成功」布爾,但我無法找回任何東西從響應別的,因爲我得到一個「對象引用不設置到對象的實例。 「我嘗試執行以下操作時出現錯誤

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

     } 

我也不能返回響應「jsonResponse」到OutputBuffer中帶有「不能隱式轉換jsonResponse到BlobColumn」

我也無法從outPutResponse.jsonResponse與以下錯誤返回的響應值。

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

的最好重載方法匹配 'System.Text.Encoding.GetBytes(炭[])' 具有一些無效參數 參數1:不能從 'Script.Response' 轉換到 '字符[]'

這是我最後的絆腳石,無法得到正確的結果。誰能幫忙?提前致謝。

回答

2

假設您使用Newtonsoft JSON.Net,因爲這是一個聰明的想法。

當實際屬性名稱爲Response時,您已將屬性名稱更改爲jsonResponse

如果要改變名稱,只要你喜歡,你有

[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代替ofc:

public Response Response { get; set; } 
+0

你不知道我現在多麼欣賞這個 –

+0

@LucasPerrett你的歡迎 – Jim