2017-12-04 18 views
0

eBay의 새로운 REST Sell API를 사용하여 인벤토리 항목을 만듭니다. 제품 측면을 수동으로 만드는 데 문제가 있습니다. 지금까지 내가 제품 측면이 알고eBay Sell API Inventory Item (.NET)을 만들 때 Aspects 객체를 serialize 할 수 없습니다.

{ 
    "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" 
     ] 
    } 
} 

: 이베이의 요청 페이로드 샘플입니다 아래

Could not serialize field [product.aspects] 

: 나는 이름 값 쌍의 목록을 작성 시도했지만 이베이는 다음과 같은 오류를 반환 고정되어 있지 않고 아무것도 될 수 없기 때문에 클래스를 만들 수 없습니다. 수동으로 JSON을 생성하고 요청 페이로드의 올바른 위치에 삽입하는 방법을 제외하고는이를 어떻게 처리해야할지 모르겠습니다.

더 좋은 방법이 있나요? 어쩌면 동적 객체를 즉시 만들 수 있습니다 (예제가 도움이 될까요?).

답변

0

확장 메서드를 사용하여 수동으로 목록을 JSON으로 변환하여 개체로 역 직렬화 한 다음 요청 페이로드를 eBay로 전달하여 Aspect 개체 목록을 만들었습니다.

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