2016-11-11 56 views
2

我想将我的De序列化JSON响应中的值返回给不同的输出,但是当我去输出它们时,我只能看到RootObject属性。Deserilized Json字符串到多个输出

在SSIS脚本组件我已经创建了如下输出 SSIS Script Component Outputs

我已经宣布了以下类。

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 Response { get; set; } 

} 

我原始的Json响应看起来像这样。

{ 
    "Success": true, 
    "Response": { 
    "Applications": [ 
     { 
     "App_ID": 1638486, 
     "App_Ref": "Test Example", 
     "Status": "Complete", 
     "Error_Code": null, 
     "Error_Message": null, 
     "Create_Dt": "2014-05-14 03:09:01.030 +00:00", 
     "Modify_Dt": "2014-05-14 03:10:59.757 +00:00", 
     "Client_Name": "Silver Chef", 
     "Client_Code": "SLVC01", 
     "Centrelink_Status": "Receiving_Logons" 
     }, 
     { 
     "App_ID": 1637906, 
     "App_Ref": "SME Demo", 
     "Status": "Complete", 
     "Error_Code": null, 
     "Error_Message": null, 
     "Create_Dt": "2015-10-08 03:07:26.793 +00:00", 
     "Modify_Dt": "2015-10-08 03:23:32.833 +00:00", 
     "Client_Name": "Silver Chef", 
     "Client_Code": "SLVC01", 
     "Centrelink_Status": "Receiving_Logons" 
     }, 
     { 
     "App_ID": 1585286, 
     "App_Ref": "Test", 
     "Status": "Receiving_Logons", 
     "Error_Code": null, 
     "Error_Message": null, 
     "Create_Dt": "2015-12-04 03:12:49.617 +00:00", 
     "Modify_Dt": "2015-12-04 03:12:49.617 +00:00", 
     "Client_Name": "Silver Chef", 
     "Client_Code": "SLVC01", 
     "Centrelink_Status": "Receiving_Logons" 
     }, 
     { 
     "App_ID": 1585398, 
     "App_Ref": "Test", 
     "Status": "Receiving_Logons", 
     "Error_Code": null, 
     "Error_Message": null, 
     "Create_Dt": "2015-12-04 03:27:59.023 +00:00", 
     "Modify_Dt": "2015-12-04 03:27:59.023 +00:00", 
     "Client_Name": "Silver Chef", 
     "Client_Code": "SLVC01", 
     "Centrelink_Status": "Receiving_Logons" 
     }, 
     { 
     "App_ID": 1585400, 
     "App_Ref": "Test", 
     "Status": "Receiving_Logons", 
     "Error_Code": null, 
     "Error_Message": null, 
     "Create_Dt": "2015-12-04 03:28:22.903 +00:00", 
     "Modify_Dt": "2015-12-04 03:28:22.903 +00:00", 
     "Client_Name": "Silver Chef", 
     "Client_Code": "SLVC01", 
     "Centrelink_Status": "Receiving_Logons" 
     } 
], 
    "Current_Dt": "2016-11-11 01:01:01.743 +00:00", 
    "Last_App_Dt": "2016-10-03 22:48:56.397 +00:00", 
    "Count": 500, 
    "Total": 1870 
    } 
} 

该方法已用于发送和接收响应是这样的,它似乎是反序列化没有错误。我可以输出“成功”属性。

private RootObject GetWebServiceResult(string vAPIUrl) 
    { 

     string vAPIToken = Variables.APIToken; 

     //Create Web Request 
     HttpWebRequest apireq = (HttpWebRequest)WebRequest.Create(vAPIUrl); 
     apireq.ContentType = "application/json"; 
     apireq.Method = "POST"; 

     string jsonPostStr = "{\"Settings\": {\"API_Token\": \"" + vAPIToken + "\"}, \"Payload\": {}}"; 
     byte[] postString = Encoding.UTF8.GetBytes(jsonPostStr); 

     apireq.ContentLength = postString.Length; 

     Stream jsonStream = apireq.GetRequestStream(); 

     jsonStream.Write(postString, 0, postString.Length); 
     jsonStream.Close(); 

     // Get Web Response   
     HttpWebResponse apirsp = (HttpWebResponse)apireq.GetResponse(); 
     RootObject jsonResponse = null; 

     Stream jsonRspStream = apirsp.GetResponseStream(); 
     string apiResponseString = null; 

     using (StreamReader reader = new StreamReader(jsonRspStream)) 
     { 
      apiResponseString = reader.ReadToEnd(); 
      Console.WriteLine(apiResponseString); 
      reader.Close(); 
     } 


     JavaScriptSerializer returnJson = new JavaScriptSerializer(); 

     //var serialJsonStr = returnJson.Serialize(apiResponseString); 
     System.Windows.Forms.MessageBox.Show(apiResponseString); 

     jsonResponse = returnJson.Deserialize<RootObject>(apiResponseString); 

     return jsonResponse; 

    } 

但是,当我尝试写入应用程序输出缓冲区返回我只看到成功和响应。 (不能输出字符串响应,但认为这是因为它超过8000个字符)

当我尝试设置每个应用程序的值时,我看到了这一点。 setting output values

我在我的foreach语句中出现以下错误。

“foreach语句无法在类型 'ScriptMain.Response' 的变量操作,因为 'ScriptMain.Response' 不包含 '的GetEnumerator' 一个 公共定义”

请帮帮忙!这让我疯狂!我究竟做错了什么?

+1

您的'Response'对象不是列举的'List'。如果你想循环Application对象,你需要去'outPutResponse.Response.Applications'。否则一切看起来不错。 – Searching

+0

请仅添加代码 – Rahul

+1

Rahul的必要/相关部分,我认为我发布的所有内容都与我遇到的错误相关。 –

回答

1

我想你想遍历是什么Response.Applications

foreach (Application app in outPutResponse.Response.Applications) 
{ 
    ApplicationBuffer.AddRow(); 
    ApplicationBuffer.AppID = app.App_ID; 
} 
+0

我能够从响应中输出“Success”bool,但我无法从响应中取回任何其他内容,因为我正在获取未设置为对象实例的** _“对象引用。”_ **当我尝试并执行以下操作时出错。 –

1

您也可以对

public class RootObject 
{ 
    public bool Success { get; set; } 
    public Response response { get; set; } 
        ^
} 

一看,另一部分是因为你希望所有的应用程序应该是outPutResponse.response.Applications in the foreach loop

and lastly

ApplicationBuffer.AppID = app.App_ID;