【问题标题】:eBay Sell API: Aspects object returns 'Could not serialize field' error, when creating Inventory ItemeBay Sell API:创建库存项目时,Aspects 对象返回“无法序列化字段”错误
【发布时间】:2017-12-04 13:12:57
【问题描述】:

我正在使用 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"
        ]
    }
}

据我所知,产品方面不是固定的,可以是任何东西,因此我无法创建类。除了手动创建 JSON 并将其插入到请求负载中的正确位置之外,我不确定如何处理此问题。

有没有更好的方法来做到这一点?也许动态创建一个动态对象(任何示例都会有所帮助)?

【问题讨论】:

    标签: .net json ebay-api


    【解决方案1】:

    我最终创建了一个 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
    

    【讨论】:

      【解决方案2】:

      我最终修改了 OpenAPI 合约来完成这项工作。 Aspect 是一个System.Collections.Generic.ICollection&lt;string&gt;,它永远不会序列化为正确的 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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多