如何使用newtonsoft获取所有json值

时间:2017-01-08 16:31:44

标签: vb.net json.net

我很难完成字符串的所有JSON值,任何人都可以帮助我吗? 我只得到一个单一的价值,我哪里错了?

我的代码

Dim address As String = "http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim json = (reader.ReadToEnd)

Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteio As Sorteio = JsonConvert.DeserializeObject(Of Sorteio)(json)


For Each nums In objsSorteio.Premios
    MsgBox(nums.ToString)
Next

Public Class Premio
    Public Property Faixa() As String
        Get
            Return m_Faixa
        End Get
        Set(value As String)
            m_Faixa = Value
        End Set
    End Property
    Private m_Faixa As String
    Public Property NumeroGanhadores() As Integer
        Get
            Return m_NumeroGanhadores
        End Get
        Set(value As Integer)
            m_NumeroGanhadores = Value
        End Set
    End Property
    Private m_NumeroGanhadores As Integer
    Public Property Valor() As Double
        Get
            Return m_Valor
        End Get
        Set(value As Double)
            m_Valor = Value
        End Set
    End Property
    Private m_Valor As Double
End Class

Public Class Sorteio
    Public Property NumSorteio() As Integer
        Get
            Return m_NumSorteio
        End Get
        Set(value As Integer)
            m_NumSorteio = Value
        End Set
    End Property
    Private m_NumSorteio As Integer
    Public Property Numeros() As List(Of Integer)
        Get
            Return m_Numeros
        End Get
        Set(value As List(Of Integer))
            m_Numeros = Value
        End Set
    End Property
    Private m_Numeros As List(Of Integer)
    Public Property Premios() As List(Of Premio)
        Get
            Return m_Premios
        End Get
        Set(value As List(Of Premio))
            m_Premios = Value
        End Set
    End Property
    Private m_Premios As List(Of Premio)
    Public Property Ganhadores() As List(Of Object)
        Get
            Return m_Ganhadores
        End Get
        Set(value As List(Of Object))
            m_Ganhadores = Value
        End Set
    End Property
    Private m_Ganhadores As List(Of Object)
End Class

Public Class RootObject
    Public Property NumeroConcurso() As Integer
        Get
            Return m_NumeroConcurso
        End Get
        Set(value As Integer)
            m_NumeroConcurso = Value
        End Set
    End Property
    Private m_NumeroConcurso As Integer
    Public Property Acumulou() As Boolean
        Get
            Return m_Acumulou
        End Get
        Set(value As Boolean)
            m_Acumulou = Value
        End Set
    End Property
    Private m_Acumulou As Boolean
    Public Property EstimativaPremio() As Double
        Get
            Return m_EstimativaPremio
        End Get
        Set(value As Double)
            m_EstimativaPremio = Value
        End Set
    End Property
    Private m_EstimativaPremio As Double
    Public Property ValorAcumulado() As Double
        Get
            Return m_ValorAcumulado
        End Get
        Set(value As Double)
            m_ValorAcumulado = Value
        End Set
    End Property
    Private m_ValorAcumulado As Double
    Public Property Data() As String
        Get
            Return m_Data
        End Get
        Set(value As String)
            m_Data = Value
        End Set
    End Property
    Private m_Data As String
    Public Property RealizadoEm() As String
        Get
            Return m_RealizadoEm
        End Get
        Set(value As String)
            m_RealizadoEm = Value
        End Set
    End Property
    Private m_RealizadoEm As String
    Public Property DescricaoAcumuladoOutro() As String
        Get
            Return m_DescricaoAcumuladoOutro
        End Get
        Set(value As String)
            m_DescricaoAcumuladoOutro = Value
        End Set
    End Property
    Private m_DescricaoAcumuladoOutro As String
    Public Property ValorAcumuladoOutro() As Double
        Get
            Return m_ValorAcumuladoOutro
        End Get
        Set(value As Double)
            m_ValorAcumuladoOutro = Value
        End Set
    End Property
    Private m_ValorAcumuladoOutro As Double
    Public Property DataProximo() As String
        Get
            Return m_DataProximo
        End Get
        Set(value As String)
            m_DataProximo = Value
        End Set
    End Property
    Private m_DataProximo As String
    Public Property ValorAcumuladoEspecial() As Double
        Get
            Return m_ValorAcumuladoEspecial
        End Get
        Set(value As Double)
            m_ValorAcumuladoEspecial = Value
        End Set
    End Property
    Private m_ValorAcumuladoEspecial As Double
    Public Property Arrecadacao() As Double
        Get
            Return m_Arrecadacao
        End Get
        Set(value As Double)
            m_Arrecadacao = Value
        End Set
    End Property
    Private m_Arrecadacao As Double
    Public Property Sorteios() As List(Of Sorteio)
        Get
            Return m_Sorteios
        End Get
        Set(value As List(Of Sorteio))
            m_Sorteios = Value
        End Set
    End Property
    Private m_Sorteios As List(Of Sorteio)
End Class

我无法通过所有“Numeros”和“Premios”

1 个答案:

答案 0 :(得分:2)

你不必要地使这种情况复杂化。

使用提供的URL,将公开的json数据复制并插入到http://jsonutils.com/等实用程序站点中。它生成了具有自动属性的模型。请注意,它将集合转换为数组,就像您在类中一样列入列表。

'''other code removed for brevity
Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteioList As List(Of Sorteio) = objs.Sorteios.ToList()

For Each objsSorteio In objsSorteioList
    For Each prems In objsSorteio.Premios
        MsgBox(prems.ToString)
    Next
    For Each nums In objsSorteio.Numeros
        MsgBox(nums.ToString)
    Next
Next

基本上与原来的相同,具有更好的可读性。如果这是您的偏好,请随意将数组转换回列表。

第一次反序化已经为你提供了必要的对象。深入了解属性以访问所需的值。

getAccountDetails(accountId: string): Promise<any> {
    let url = API_ENDPOINT + 'some-string';
    return new Promise(resolve => {
      this.userData.hasLoggedIn().then((hasLoggedIn) => {
        if (hasLoggedIn) {
          this.userData.getUserToken().then((token) => {
            let headers = new Headers();

            headers.append('Authorization', 'Bearer ' + token);

            this.http.post(url, { id: accountId }, { headers: headers }).map(res => res.json()).subscribe(data => {
              resolve(data);
            });
          })
        }
      })
    });
  }