Access Data in Dictionary with deserialized data

57 views Asked by At

My program accesses data through a Web API and gets data in JSON format. Response is for example:

{
   "response":{
      "stationId":"24026900",
      "prices":[
         {
            "name":"Aral Super E10",
            "price":"146,90",
            "currency":"EUR",
            "id":"001131",
            "sort":"21"
         },
         {
            "name":"Aral Super 95",
            "price":"152,90",
            "currency":"EUR",
            "id":"001040",
            "sort":"22"
         },
         {
            "name":"Aral Ultimate 102",
            "price":"172,90",
            "currency":"EUR",
            "id":"001255",
            "sort":"24"
         },
         {
            "name":"Aral Diesel",
            "price":"130,90",
            "currency":"EUR",
            "id":"004002",
            "sort":"30"
         },
         {
            "name":"Aral Ultimate Diesel",
            "price":"150,90",
            "currency":"EUR",
            "id":"004267",
            "sort":"31"
         },
         {
            "name":"Aral LKW Diesel",
            "price":"130,90",
            "currency":"EUR",
            "id":"004010",
            "sort":"32"
         }
      ],
      "lastUpdate":"202104122030",
      "disabled":"false",
      "openNow":"Wir haben f\u00fcr Sie ge\u00f6ffnet."
   }
}

How can I e.g. access deeply nested data like "price":"172,90".

My structure is like:

Dim sJSON As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim PriceLastValue As String = funcGetPriceInfo("response") 'How to get access to deeper nested data here?
    '
    'Every key except "response" throws an exception
    '
    MsgBox(PriceLastValue.ToString)
End Sub

Private Function funcGetPriceInfo(ByVal sVal As String) As String
    Dim JSONSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
    sJSON = ReadStreamFromWeb.ReadFileFromWeb_WebRequest("http://ap.aral.de/api/v2/getStationPricesById.php?stationId=24026900")
    Dim dictJSON As Dictionary(Of String, Object) = JSONSerializer.Deserialize(Of Dictionary(Of String, Object))(sJSON)
    Return dictJSON(sVal).ToString
End Function
1

There are 1 answers

1
fox On

thank you, really appreciate your help! Now added reference to Newtonsoft and the classes:

Public Class Rootobject
    Public Property response As Response
End Class

Public Class Response
    Public Property stationId As String
    Public Property prices() As Price
    Public Property lastUpdate As String
    Public Property disabled As String
    Public Property openNow As String
End Class

Public Class Price
    Public Property name As String
    Public Property price As String
    Public Property currency As String
    Public Property id As String
    Public Property sort As String
End Class

And trying to get the object:

    sJSON = ReadStreamFromWeb.ReadFileFromWeb_WebRequest("http://ap.aral.de/api/v2/getStationPricesById.php?stationId=24026900")
    Dim obj As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(sJSON)

But this throws..

Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'myprog.Form1+Price' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.