2017-01-08 9 views
0

文字列のすべてのJSON値を調べるのに苦労しています。 私はただ一つの価値しか得ていませんが、どこが間違っていますか?私も、すべて "Numeros" と "Premios"newtonsoftですべてのjson値を取得する方法

+2

自動実装プロパティでは、定型記号をすべて記述する必要がありません。 jsonは 'RootObject'オブジェクト全体を表しているので、最初のデシリアライザコードだけが動作するはずです。 'objs.Sorteios.Premios'はあなたの後ろにあるようです – Plutonix

答えて

2

を通過することはできません

マイコード

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 

あなたは不必要にこれを複雑に超えています。

URLを使用すると、公開されたjsonデータがコピーされ、http://jsonutils.com/のようなユーティリティサイトに接続されました。これは、自動プロパティを持つモデルを生成しました。クラスにあるように、コレクションを配列にapposedとしてListに変換したことに注意してください。

Public Class Premio 
    Public Property Faixa As String 
    Public Property NumeroGanhadores As Integer 
    Public Property Valor As Double 
End Class 

Public Class Sorteio 
    Public Property NumSorteio As Integer 
    Public Property Numeros As Integer() 
    Public Property Premios As Premio() 
    Public Property Ganhadores As Object() 
End Class 

Public Class RootObject 
    Public Property NumeroConcurso As Integer 
    Public Property Acumulou As Boolean 
    Public Property EstimativaPremio As Double 
    Public Property ValorAcumulado As Double 
    Public Property Data As String 
    Public Property RealizadoEm As String 
    Public Property DescricaoAcumuladoOutro As String 
    Public Property ValorAcumuladoOutro As Double 
    Public Property DataProximo As String 
    Public Property ValorAcumuladoEspecial As Double 
    Public Property Arrecadacao As Double 
    Public Property Sorteios As Sorteio() 
End Class 

基本的に元々の読みやすさと同じです。あなたの好みであれば、リストをリストに戻して自由に変換してください。

最初の審査は、すでに必要なオブジェクトを提供しています。必要な値にアクセスするには、プロパティにドリルダウンします。

'''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 
関連する問題