如何使用Newtonsoft从json字符串中获取所有值

时间:2017-01-05 23:04:13

标签: json vb.net

我使用Newtonsoft类来阅读JSON文本,而且我很难获得所有的值,任何人都可以帮助我,我该怎么做?

地址是这样的 https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json

我的代码试试

Dim req As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim sr As StreamReader

Dim json As String

req = WebRequest.Create("https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json")
response = req.GetResponse()

sr = New StreamReader(response.GetResponseStream())
json = sr.ReadToEnd

Dim o As JObject = JObject.Parse(json)

For Each t As JToken In o.Descendants()
    MsgBox(t(1).ToString)
Next

1 个答案:

答案 0 :(得分:1)

创建一个模型来存储反序列化的数据

Public Class Estado
    Public Property sigla As String
    Public Property nome As String
    Public Property cidades As String()
End Class

Public Class Data
    Public Property estados As Estado()
End Class

然后反序列化

Dim result As Data = JsonConvert.DeserializeObject(Of Data)(json)

现在可以访问数据

Dim firstItemName As String = result(0).nome