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

时间:2023-02-05 20:44:32

I'm using the Newtonsoft class to read a JSON text, and I'm having difficulty getting all the values, can anyone help me with how can I do it?

我正在使用Newtonsoft类来读取JSON文本,并且我很难获得所有的值,任何人都可以帮助我如何做到这一点?

The address is this https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json

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

My code try

我的代码试试

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 个解决方案

#1


1  

Create a model to store the deserialized data

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

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

then deserialize it

然后反序列化它

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

Now data can be accessed

现在可以访问数据

Dim firstItemName As String = result(0).nome

#1


1  

Create a model to store the deserialized data

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

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

then deserialize it

然后反序列化它

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

Now data can be accessed

现在可以访问数据

Dim firstItemName As String = result(0).nome