簡體   English   中英

C# 從 API 響應中提取數據,其中響應似乎是 Object 與 Z3501BB093D963810B8671

[英]C# Extracting Data from API Response where response seems to be an Object vs XML

我調用 API 的方式與我一直調用 API 的方式相同。 除了我使用過的所有其他 API 之外,響應是我所期望的,它返回了一個 XML 字符串,該字符串很容易從中獲取數據。 這個特殊的 API 似乎返回響應並將響應數據放入 object。 這是我在 C# 中調用 API 的方式...

KeyFieldsAPI.PS_API_KeyFields_V2 KeyFieldsAPI = new KeyFieldsAPI.PS_API_KeyFields_V2();
object KFAPI = KeyFieldsAPI.GetKeyFields(KAccount_Number, KCourier, KService, "", InventoryCodeList);

我可以循環通過 KFAPI object 並使用以下方法將上層字段響應放入變量中:

foreach (PropertyInfo property in KFAPI.GetType().GetProperties())
{
    PropName = property.Name.ToString();
    PropType = property.GetType().ToString();

        PropValue = property.GetValue(KFAPI, null).ToString();
        DataRow dr = ITAB_KFHEADER.NewRow();
        dr["Name"] = PropName;
        dr["Value"] = PropValue;
        ITAB_KFHEADER.Rows.Add(dr);
}

但是,其中兩個字段似乎是 arrays ,如此處所示...手表值框的圖像

我需要從 NeedDate_Item 字段中提取數據,該字段似乎是一個包含 3 個索引數據行的數組,作為 object 的一部分。 誰能建議我如何從作為嵌入式陣列一部分的 API 響應 object 中提取數據? 提前感謝大家。

我想到了。 如果我更改代碼行以將 API 從 object 調用為 var,如下所示:

var KFAPI = KeyFieldsAPI.GetKeyFields(KAccount_Number, KCourier, KService, "", InventoryCodeList);

然后它允許我像這樣抓取數據:

foreach (var NDdate in KFAPI.NeedDate_Item) 
{ *** do work here*** 
}

使用“對象”調用 API 不允許我使用 select 並使用嵌入式 arrays 但將調用更改為“var”確實如此。 也許這可能會在未來對其他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM