简体   繁体   中英

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. 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. This one particular API seems to return the response and place the response data into an object. Here is how I'm calling the API in C#...

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:

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

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. Can anyone suggest how I can extract data from this API response object that is part of an embedded array? 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:

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. Maybe this might help someone else in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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