简体   繁体   English

简单的JSON反序列化为假人

[英]Simple JSON deserialisation for dummies

Trying to deserialise the following json returned from a web source: 尝试反序列化从Web源返回的以下json:

{
    "cards": [
    {
        "high": "8.27", 
        "volume": 5, 
        "percent_change": "0.00", 
        "name": "Wurmcoil engine", 
        "url": "http://blacklotusproject.com/cards/Scars+of+Mirrodin/Wurmcoil+Engine/", 
        "price": "6.81", 
        "set_code": "SOM", 
        "average": "5.67", 
        "change": "0.00", 
        "low": "1.12"}], 
        "currency": "USD"
    }

I am using json.net with visual basic, new to both of them, especially the object oriented portions of vb. 我正在使用带有visual basic的json.net,对它们都是新的,尤其是vb的面向对象部分。 I would just like to extract the 'price' variable. 我只想提取'价格'变量。 I have set up a class as such: 我已经建立了一个类:

Public Class Card
    Public high As String
    Public volume As String
    Public percent_change As String
    Public name As String
    Public url As String
    Public price As String
    Public set_code As String
    Public average As String
    Public change As String
    Public low As String
End Class

The code I am currently using is: 我目前使用的代码是:

Public Sub parse_json(url As String)
    Dim blp_json As String = ""
    Dim wClient As New WebClient
    wClient.Proxy = System.Net.HttpWebRequest.DefaultWebProxy
    blp_json = wClient.DownloadString(url)
    MessageBox.Show(blp_json)
    Dim card_1 = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Card)(blp_json)
    PriceTextBox.Text = card_1.price
    TextBox1.AppendText(card_1.ToString)
    TextBox1.AppendText(blp_json)

End Sub

Just trying many different things to get a hang of it, not really sure what I am doing. 只是尝试了许多不同的东西来掌握它,不确定我在做什么。 I presume my Card class is incorrect as 'price' appears to be nested in cards:[{...}] 我认为我的卡类不正确,因为'价格'似乎嵌套在卡片中:[{...}]

I don't really know about deserialising json at all, and much less about how to do it /properly/ in vb. 我根本不知道对json进行反序列化,更不用说如何在vb中正确/正确地进行反序列化了。

I use the System.Web.Script.Serialization.JavaScriptSerializer for JSON deserialization. 我使用System.Web.Script.Serialization.JavaScriptSerializer进行JSON反序列化。 Your example is slightly complicated by the fact that cards contains an array of JSON objects. 由于cards包含JSON对象数组,因此您的示例稍微复杂一些。 This is indicated by the "[" and "]". 这由“[”和“]”表示。 This example code will show you how to process it regardless of whether cards is an array or not. 此示例代码将向您展示如何处理它,无论cards是否是数组。 You may wish to ignore the Else if you are sure that there will always be an array in cards 如果你确定cards总会有一个数组,你可能希望忽略Else

Make sure that you have included a reference to System.Web.Extensions in your project and... 确保在项目中包含对System.Web.Extensions的引用,并且...

Imports System.Web.Script.Serialization

and then... 接着...

Dim MySerializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim dictResult As Dictionary(Of String, Object) = MySerializer.Deserialize(Of Dictionary(Of String, Object))(blp_json)

Dim dictCard As Dictionary(Of String, Object)

If dictResult.ContainsKey("cards") Then

    If TypeOf dictResult("cards") Is ArrayList Then

        Dim arrResult As ArrayList = DirectCast(dictResult("cards"), ArrayList)

        For Each arrCardRecord In arrResult

            dictCard = DirectCast(arrCardRecord, Dictionary(Of String, Object))

            If dictCard.ContainsKey("price") Then Console.WriteLine(dictCard("price"))

        Next
    Else
        dictCard = DirectCast(dictResult("cards"), Dictionary(Of String, Object))
        If dictCard.ContainsKey("price") Then Console.WriteLine(dictCard("price"))
    End If

End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM