简体   繁体   English

C# 从 API 响应中提取数据,其中响应似乎是 Object 与 Z3501BB093D963810B8671

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

I'm calling an API the same way I always have called APIs.我调用 API 的方式与我一直调用 API 的方式相同。 The response is what I would expect except with every other API I've used it's returned an XML string which is easy to get data out of.除了我使用过的所有其他 API 之外,响应是我所期望的,它返回了一个 XML 字符串,该字符串很容易从中获取数据。 This one particular API seems to return the response and place the response data into an object.这个特殊的 API 似乎返回响应并将响应数据放入 object。 Here is how I'm calling the API in C#...这是我在 C# 中调用 API 的方式...

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

I can loop through the KFAPI object and place the upper level field responses into variables by using:我可以循环通过 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);
}

However two of the fields seem to be arrays as seen here... image of watch values box但是,其中两个字段似乎是 arrays ,如此处所示...手表值框的图像

I need to extract data from the NeedDate_Item field which seems to be an array with 3 index lines of data as part of this object.我需要从 NeedDate_Item 字段中提取数据,该字段似乎是一个包含 3 个索引数据行的数组,作为 object 的一部分。 Can anyone suggest how I can extract data from this API response object that is part of an embedded array?谁能建议我如何从作为嵌入式阵列一部分的 API 响应 object 中提取数据? Thanks to everyone in advance.提前感谢大家。

I figured it out.我想到了。 If I change the line of code to call the API from object to var like this:如果我更改代码行以将 API 从 object 调用为 var,如下所示:

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

Then it allows me to grab the data like this:然后它允许我像这样抓取数据:

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

Calling the API with "object" did not allow me to select and use the embedded arrays but changing the call to "var" did.使用“对象”调用 API 不允许我使用 select 并使用嵌入式 arrays 但将调用更改为“var”确实如此。 Maybe this might help someone else in the future.也许这可能会在未来对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM