Json 使用反序列化数据访问字典中的数据
我的程序通过Web API访问数据,并以JSON格式获取数据。例如,答复如下:Json 使用反序列化数据访问字典中的数据,json,vb.net,javascriptserializer,Json,Vb.net,Javascriptserializer,我的程序通过Web API访问数据,并以JSON格式获取数据。例如,答复如下: { "response":{ "stationId":"24026900", "prices":[ { "name":"Aral Super E10", "price":"1
{
"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."
}
}
例如,如何访问深度嵌套的数据,如“price”:“172,90”
我的结构是:
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
谢谢你,非常感谢你的帮助!现在添加了对Newtonsoft和类的引用:
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
并尝试获取对象:
sJSON = ReadStreamFromWeb.ReadFileFromWeb_WebRequest("http://ap.aral.de/api/v2/getStationPricesById.php?stationId=24026900")
Dim obj As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(sJSON)
但这会让
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.
这是一个简单的结构。根对象
response
包含5个属性。prices
属性是Price
对象的数组或列表。您可以通过索引访问数组中的每个对象。-顺便说一句,您读取的JSON响应编码错误(请参阅openNow
属性值,Wir haben f\u00fcr
应该是Wir haben für
)。您的JSON是UTF-8编码的。-您可能应该将此JSON反序列化为类模型。-您需要使用JavaScriptSerializer吗?公共财产价格()作为(价格)列表
。属性的这种格式:Public Property prices()As Price
具有误导性(因为在这种语言中属性中使用了多余的括号,而不是必需的括号),它定义了Price
类型的单个对象,而不是数组。