从网络服务器下载文件

Download Files From Web Server

我收到来自 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 的代码如下:

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

我正在使用 json.net Visual Basic 2008。

这些是我的 类

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 结果的代码

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' 类型,不是集合类型。

如何修复此错误?

几点可以为您提供工作代码:

  • 有一种更简单的方法可以下载 JSON 数据。
  • 你不小心 将 RootObject.files 声明为 列表数组 ,其 GetSet 方法是空的。
  • File 是一个不幸的名字 class 作为 它与 System.IO.File.
  • 冲突
  • 最好有 (我命名的)FileData 中的东西作为属性。你可以采取 利用自动声明的属性,而不必键入 Get/Set 方法。

将所有这些放在一起,我得出了

 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

输出

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

并保存文件。