Reputation: 11
I have problem to get "kode" from "list" as array from this object.
{
"metaData":{
"code":"200",
"message":"Sukses"
},
"response":{
"list":[
{
"kode":"31486",
"nama":"Satro Jadhit, dr"
},
{
"kode":"31492",
"nama":"Satroni Lawa, dr"
}
]
}
}
i can get anything except array from JSON. i've tried to use this solution [How to read JSON http post response using VB
and other solution too but my vb don't have some feature from newtonsoft json. I use visual studio 2005, .net framework 2.0
So, how I get it as my array.
Edit
this is what i've tried, and I can get value from the "list" (i use another code, so this value it's different)
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Dim kodeDpjp as string
Dim json As String = responseFromServer
Dim serObj As Newtonsoft.Json.Linq.JObject =
Newtonsoft.Json.Linq.JObject.Parse(json)
Dim token As Newtonsoft.Json.Linq.JToken = serObj.SelectToken("response")
("list")
If (token.GetType() Is GetType(Newtonsoft.Json.Linq.JArray)) Then
Console.WriteLine(token)
End If
and this is the result
[
{
"kode": "8784",
"nama": "drg.MELANI SARI TANJUNG"
},
{
"kode": "8848",
"nama": "drg.ARIEF KURNIAWAN"
},
{
"kode": "8873",
"nama": "drg.SRI ARIANI SUGIARTI"
}
]
i just want "kode" for my array in combobox
Thank you
Upvotes: 0
Views: 2894
Reputation: 129707
Since you know that the result of serObj.SelectToken("response")("list")
is a JArray
, you can cast it using CType
and then use a For Each
loop to iterate over the items. Each item here is a JObject
and you can use the indexer method to get the property values. Then you can use CType
again to convert those values to strings.
Here it is in code:
' Parse the JSON to a JObject
Dim serObj As JObject = JObject.Parse(json)
' Retrieve the list and cast it to a JArray
Dim list As JArray = CType(serObj.SelectToken("response")("list"), JArray)
' Loop over the items in the array (each is a JObject)
For Each item As JObject In list
' Get the "kode" property from the JObject and convert it to a string
Dim kode As String = CType(item("kode"), String)
' Get the "nama" property from the JObject and convert it to a string
Dim nama As String = CType(item("nama"), String)
' You can add kode and/or nama to your ComboBox here
Next
Fiddle: https://dotnetfiddle.net/lkrWa4
Upvotes: 1