0

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

3
  • i've tried to use this solution... in that case, please edit your question to share what you tried that did not work. For why, see How to Ask which suggests Help others reproduce the problem. Commented Oct 11, 2018 at 16:51
  • @BrianRogers i use json.net .net 2.0 Commented Oct 11, 2018 at 23:18
  • @dbc thank you, i've edited my question. Commented Oct 11, 2018 at 23:18

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.