eBay Sell API:Aspects 对象 returns 'Could not serialize field' 错误,创建库存项目时
eBay Sell API: Aspects object returns 'Could not serialize field' error, when creating Inventory Item
我正在使用 eBay 的新 REST Sell API 创建库存商品。我在手动创建产品方面时遇到问题。我已尝试创建名称值对列表,但 eBay 返回以下错误:
Could not serialize field [product.aspects]
以下是来自 eBay 的请求负载示例:
{
"availability": {
"shipToLocationAvailability": {
"quantity": 50
}
},
"condition": "NEW",
"product": {
"title": "GoPro Hero4 Helmet Cam",
"description": "New GoPro Hero4 Helmet Cam. Unopened box.",
"aspects": {
"Brand": [
"GoPro"
],
"Type": [
"Helmet/Action"
],
"Storage Type": [
"Removable"
],
"Recording Definition": [
"High Definition"
],
"Media Format": [
"Flash Drive (SSD)"
],
"Optical Zoom": [
"10x"
]
},
"imageUrls": [
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1000.jpg",
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1001.jpg",
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1002.jpg"
]
}
}
据我所知,产品方面不是固定的,可以是任何东西,因此我无法创建 Class。除了手动创建 JSON 并将其插入请求负载中的正确位置之外,我不确定如何处理此问题。
有更好的方法吗?也许动态创建一个动态对象(任何示例都会有所帮助)?
我最终创建了一个 Aspect 对象的列表,使用扩展方法手动将列表转换为 JSON,我将其反序列化为一个对象并将请求有效负载传递给 eBay。
Public Class Aspect
Property Name As String
Property Values As String()
End Class
Public Class RequestPayload
<JsonProperty("aspects")>
Public Property Aspects As Object
End Class
Sub Click()
Dim Payload As New RequestPayload
Payload.Aspects = (New List(Of Aspect) From {
New Aspect With {.Name = "Brand", .Values = {"GoPro"}},
New Aspect With {.Name = "Type", .Values = {"Helmet/Action"}}
}
).ToAspectsObject()
End Sub
<Extension()>
Function ToAspectsObject(Source As List(Of Aspect)) As Object
Dim Aspects As New List(Of String)
For Each Aspect In Source
Aspects.Add(String.Format("""{0}"": [{1}]", Aspect.Name, String.Join(", ", Aspect.Values.Select(Function(x) String.Format("""{0}""", x)))))
Next
Dim JsonObject = String.Format("{{{0}}}", String.Join(", ", Aspects))
Return Json.DeserializeObject(JsonObject)
End Function
我最终修改了 OpenAPI 合同来完成这项工作。
Aspect 是一个 System.Collections.Generic.ICollection<string>
,它永远不会序列化为正确的 JSON,无论你放在那里。我把它改成了dynamic
类型。
[Newtonsoft.Json.JsonProperty("aspects", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
//public System.Collections.Generic.ICollection<string> Aspects { get; set; }
// EDITED
public dynamic Aspects { get; set; }
然后添加到方面,这是我的代码:
inventoryItem.Product.Aspects = new System.Dynamic.ExpandoObject();
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add("Manufacturer", new List<string> {"Inoxia Ltd"});
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var1_Title_en, new List<string>
{ inventory.Var1_Value_en });
if (inventory.Var2_Title_en != "")
{
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var2_Title_en, new List<string>{ inventory.Var2_Value_en });
}
这可能会做得更整洁一些,但它可以正常工作并正确上传到 eBay。
我正在使用 eBay 的新 REST Sell API 创建库存商品。我在手动创建产品方面时遇到问题。我已尝试创建名称值对列表,但 eBay 返回以下错误:
Could not serialize field [product.aspects]
以下是来自 eBay 的请求负载示例:
{
"availability": {
"shipToLocationAvailability": {
"quantity": 50
}
},
"condition": "NEW",
"product": {
"title": "GoPro Hero4 Helmet Cam",
"description": "New GoPro Hero4 Helmet Cam. Unopened box.",
"aspects": {
"Brand": [
"GoPro"
],
"Type": [
"Helmet/Action"
],
"Storage Type": [
"Removable"
],
"Recording Definition": [
"High Definition"
],
"Media Format": [
"Flash Drive (SSD)"
],
"Optical Zoom": [
"10x"
]
},
"imageUrls": [
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1000.jpg",
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1001.jpg",
"http://i.ebayimg.com/images/i/182196556219-0-1/s-l1002.jpg"
]
}
}
据我所知,产品方面不是固定的,可以是任何东西,因此我无法创建 Class。除了手动创建 JSON 并将其插入请求负载中的正确位置之外,我不确定如何处理此问题。
有更好的方法吗?也许动态创建一个动态对象(任何示例都会有所帮助)?
我最终创建了一个 Aspect 对象的列表,使用扩展方法手动将列表转换为 JSON,我将其反序列化为一个对象并将请求有效负载传递给 eBay。
Public Class Aspect
Property Name As String
Property Values As String()
End Class
Public Class RequestPayload
<JsonProperty("aspects")>
Public Property Aspects As Object
End Class
Sub Click()
Dim Payload As New RequestPayload
Payload.Aspects = (New List(Of Aspect) From {
New Aspect With {.Name = "Brand", .Values = {"GoPro"}},
New Aspect With {.Name = "Type", .Values = {"Helmet/Action"}}
}
).ToAspectsObject()
End Sub
<Extension()>
Function ToAspectsObject(Source As List(Of Aspect)) As Object
Dim Aspects As New List(Of String)
For Each Aspect In Source
Aspects.Add(String.Format("""{0}"": [{1}]", Aspect.Name, String.Join(", ", Aspect.Values.Select(Function(x) String.Format("""{0}""", x)))))
Next
Dim JsonObject = String.Format("{{{0}}}", String.Join(", ", Aspects))
Return Json.DeserializeObject(JsonObject)
End Function
我最终修改了 OpenAPI 合同来完成这项工作。
Aspect 是一个 System.Collections.Generic.ICollection<string>
,它永远不会序列化为正确的 JSON,无论你放在那里。我把它改成了dynamic
类型。
[Newtonsoft.Json.JsonProperty("aspects", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
//public System.Collections.Generic.ICollection<string> Aspects { get; set; }
// EDITED
public dynamic Aspects { get; set; }
然后添加到方面,这是我的代码:
inventoryItem.Product.Aspects = new System.Dynamic.ExpandoObject();
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add("Manufacturer", new List<string> {"Inoxia Ltd"});
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var1_Title_en, new List<string>
{ inventory.Var1_Value_en });
if (inventory.Var2_Title_en != "")
{
((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var2_Title_en, new List<string>{ inventory.Var2_Value_en });
}
这可能会做得更整洁一些,但它可以正常工作并正确上传到 eBay。