所以我有这个结构:
type AllShipments struct {
Shipments []struct {
ShipmentID int `json:"shipmentId"`
ShipmentDate time.Time `json:"shipmentDate"`
ShipmentItems []struct {
OrderItemID string `json:"orderItemId"`
OrderID string `json:"orderId"`
} `json:"shipmentItems"`
Transport struct {
TransportID int `json:"transportId"`
} `json:"transport"`
} `json:"shipments"`
}
我想获取仅包含shipmentID值的列表,因为我需要它进行API调用以获取单个货件的特定详细信息。然后,我可以在API调用中对其进行循环。
在Python中,我这样做是
# Create shipments dataframe
df_shipments = json_normalize(full_df['shipments'], 'shipmentItems', columns)
# Turn into list so we can loop over the shipmentId property in our API call to get more detailed information
ship_list = df_shipments['shipmentId'].tolist()
功能非常强大,只有两行代码。但是发生了很多魔术。我想对Golang做同样的事情。从文档和一些搜索中,我想到了这个主意:
var shipmentList []string
for _, v := range t.Shipments {
shipmentList = append(shipmentList, string(v.ShipmentID))
}
然后shipmentList
应该返回一个包含所有shippingID的数组。但我得到以下输出:[� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �]
有人可以澄清正在发生的事情以及为什么我得到这个结果吗?以及如何获得我想要的预期结果?