【问题标题】:How to deserialize JSON structure?如何反序列化 JSON 结构?
【发布时间】:2014-10-07 21:27:52
【问题描述】:

我收到这样格式的信息

  {"UFs":   [
     {
     "Valor": "24.184,92",
     "Fecha": "2014-10-07"
     }
   ]}

我想访问此处显示的值和日期。

我有课

Public Class clValores
    Private pTipoValores As String
    Private pValores As String
    Public Property Fecha() As String
        Get
            Return pTipoValores
        End Get
        Set(ByVal value As String)
            pTipoValores = value
        End Set
    End Property
    Public Property Valor() As String
        Get
            Return pValores
        End Get
        Set(ByVal value As String)
            pValores = value
        End Set
    End Property
End Class

还有这段代码

Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://api.sbif.cl/api-sbifv3/recursos_api/uf?apikey=xxxxxxxxxxxxxxxxxxxxxxx&formato=JSON")

Dim Valor As clValores = JsonConvert.DeserializeObject(Of clValores)(result)

Response.Write(Valor.Fecha)
Response.Write(Valor.Valor)

但是在运行代码时,Valor 什么都不是。我错过了什么

【问题讨论】:

    标签: asp.net json vb.net serialization


    【解决方案1】:

    从我在这个post 中读到的内容,它就像简单地包装 UFs 参数一样简单,因此创建一个包含的额外类

    Public Class UFsContainer
        public UFs as List(Of clValores)
    End Class
    

    然后使用新类而不是 clValores 对其进行反序列化

    【讨论】:

    • 我试过这种方式 Dim Valor As UFsContainer = JsonConvert.DeserializeObject(Of UFsContainer)(result) 但我什么也没得到
    • 对不起,我刚刚看到 clValores 是一个数组,所以它应该是一个 List(Of clValores) 来正确反序列化它
    【解决方案2】:

    Imports System.Collections.Generic 添加到您的项目中并创建另一个类,如下所示 -

    Public Class UFsContainer
        public UFs as List(Of clValores)()
    End Class
    

    然后反序列化为-

    Dim Valor As UFsContainer = JsonConvert.DeserializeObject(Of UFsContainer)(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多