2015-02-04 2 views
0

URL에서 다음과 같은 응답이 있습니다. 두 개의 파일을 이름 = id로 하드 드라이브에 다운로드하려면 어떻게 코딩해야합니까?파일 다운로드 웹 서버

HTTP/1.1 200 OK 
Content-Type: application/json 

{ 
    "files": [ 
    { 
     "format": "fillz-order-tab", 
     "checksum": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", 
     "acknowledged": false, 
     "uri": "https://file-api.fillz.com/v1/orders/created/20140611T003336Z-8b975127", 
     "date_created": "20140611T003336Z", 
     "id": "20140611T003336Z-8b975127" 
    }, 
    { 
     "format": "fillz-order-tab", 
     "checksum": "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35", 
     "acknowledged": false, 
     "uri": "https://file-api.fillz.com/v1/orders/created/20140611T013545Z-3e2f2083", 
     "date_created": "20140611T013545Z", 
     "id": "20140611T013545Z-3e2f2083" 
    } 
    ] 
} 
URL을 호출

내 코드는 다음과 같은 : 나는 Visual Basic에서 2008 년

와 json.net 사용하고

Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse) 
        Dim reader As New StreamReader(response.GetResponseStream()) 
        result = reader.ReadToEnd() 

를이 내 클래스입니다

Public Class file 
    Public format As String 
    Public checksum As String 
    Public acknowledged As String 
    Public uri As String 
    Public date_created As String 
    Public id As String 
End Class 


Public Class RootObject 
    Public Property files() As List(Of file) 
     Get 

     End Get 
     Set(ByVal value As List(Of file)) 

     End Set 

    End Property 
End Class 

이것은 json 결과를 deserializare하는 코드입니다.

,451,515,
Dim res As RootObject = JsonConvert.DeserializeObject(Of FillzAPI.FileAPI.RootObject)(result) 

가 나는 URL 응답에서 각 ID를 읽을 수

For Each Data As FileAPI.RootObject In res 

Next 

나는 다음 오류가 있습니다

표현 유형 'FillzAPI.FileAPI.RootObject'이다 컬렉션 형식이 아닙니다 .

어떻게이 오류를 해결할 수 있습니까? 당신에게 작업 코드를 제공합니다

+0

표시되는 응답은 JSON처럼 보입니다. 당신은'uri' 값을 추출 할 수 있도록 파싱하는 방법을 연구 할 필요가 있습니다. –

+0

감사합니다 앤드류, 당신은 나에게 힌트를주었습니다. 그러나 나는 아직도이 코드를 고수하고있다. – Berenice

답변

0

몇 가지 포인트 :

  • JSON 데이터를 다운로드 할 수있는 쉬운 방법이 있습니다.
  • 당신은 목록의 배열로 RootObject.files을 선언 실수 한, 그 GetSet 방법은 비어 있습니다.
  • File은 클래스의 불행한 이름으로 입니다. System.IO.File과 충돌합니다.
  • (내 이름은 무엇입니까?) FileData을 속성으로 사용하는 것이 좋습니다. 자동 선언 된 속성의 이점을 취할 수 있으며 Get/Set 메서드를 입력 할 필요가 없습니다. 함께 모든 퍼팅

, 나는

https://file-api.fillz.com/v1/orders/created/20140611T003336Z-8b975127
https://file-api.fillz.com/v1/orders/created/20140611T013545Z-3e2f2083

를 출력하고 파일을 저장

Option Infer On 

Imports System.IO 
Imports System.Net 
Imports Newtonsoft.Json 

Module Module1 

    Public Class FileData 
     Public Property format As String 
     Public Property checksum As String 
     Public Property acknowledged As String 
     Public Property uri As String 
     Public Property date_created As String 
     Public Property id As String 
    End Class 

    Public Class RootObject 
     Public Property Files As List(Of FileData) 
    End Class 

    Sub Main() 
     ' I set this up on a local web server. Adjust as required. 
     Dim src = "http://127.0.0.1/JsonSample.txt" 

     ' An easy way to get a string from a web server... 
     Dim wc As New WebClient 
     'TODO: Try..Catch any error that wc.DownloadString throws. 
     Dim jsonData = wc.DownloadString(src) 

     'TODO: Try..Catch any error that JsonConvert.DeserializeObject throws. 
     Dim y = JsonConvert.DeserializeObject(Of RootObject)(jsonData) 

     ' Somewhere to save the downloaded files... 
     Dim dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "My JSON test") 
     If Not Directory.Exists(dest) Then 
      Directory.CreateDirectory(dest) 
     End If 

     For Each x In y.Files 
      Console.WriteLine(x.uri) 
      'TODO: Try..Catch any error that wc.DownloadFile throws. Also perhaps use async methods. 
      wc.DownloadFile(x.uri, Path.Combine(dest, x.id)) 
     Next 

     Console.ReadLine() 

    End Sub 

End Module 

에 도착했다.

+0

도움 주셔서 감사합니다 !!! – Berenice