VB .Net 中的 JSON.Net 反序列化 [英] JSON.Net Deserialization in VB .Net

查看:28
本文介绍了VB .Net 中的 JSON.Net 反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网站获取数据并填充应用程序列表视图中的某些字段.我读了几篇文章,我很困惑在 JavaScriptSerializer 和 JSON.Net 之间进行选择.解决此问题的最佳方法是什么?

这是来自网站的 JSON 响应

<代码>{变化": {变体":[{"id": "AQ5929_580","avStatusQuantity": 1.0,"avStatus": "NOT_AVAILABLE",库存":假,avLevels":{IN_STOCK":0.0,预购":0.0,欠款":0.0,"NOT_AVAILABLE": 1.0,预览":1.0},ATS":0.0,"inStockDate": "2016 年 6 月 14 日星期二 00:00:00 GMT"},{"id": "AQ5929_590","avStatusQuantity": 1.0,"avStatus": "NOT_AVAILABLE",库存":假,avLevels":{IN_STOCK":0.0,预购":0.0,欠款":0.0,"NOT_AVAILABLE": 1.0,预览":1.0},ATS":0.0,"inStockDate": "2016 年 6 月 14 日星期二 00:00:00 GMT"},{"id": "AQ5929_600","avStatusQuantity": 1.0,"avStatus": "NOT_AVAILABLE",库存":假,avLevels":{IN_STOCK":0.0,预购":0.0,欠款":0.0,"NOT_AVAILABLE": 1.0,预览":1.0},ATS":0.0,"inStockDate": "2016 年 6 月 14 日星期二 00:00:00 GMT"}]}}

我只想要 id、inStock、ATS 字段的值.

到目前为止,我的 VB .Net 代码,

_公共类变体私有 _id 作为字符串 = 无公共属性 id() 作为字符串得到返回_id结束获取设置(值作为字符串)_id = 值结束集最终财产私有 _avStatusQuantity 作为双倍公共属性 avStatusQuantity() As Double得到返回 _avStatusQuantity结束获取设置(值双倍)_avStatusQuantity = 值结束集最终财产私有 _avStatus 作为字符串 = 无公共属性 avStatus() 作为字符串得到返回_avStatus结束获取设置(值作为字符串)_avStatus = 值结束集最终财产Private _inStock As Boolean = False公共属性 inStock() As Boolean得到返回_inStock结束获取设置(值作为布尔值)_inStock = 价值结束集最终财产私有 _ATS 作为字符串 = 无公共属性 ATS() As Double得到返回_ATS结束获取设置(值双倍)_ATS = 值结束集最终财产Private _inStockDate As String = 无公共属性 inStockDate() As String得到返回_inStockDate结束获取设置(值作为字符串)_inStockDate = 值结束集最终财产结束类<可序列化>_公开课变化Private _Variants 作为变体公共属性变体作为变体得到返回_Variants结束获取设置(值作为变体)_Variants = 值结束集最终财产结束类

当我使用 JSONConvert 函数进行反序列化时,结果总是没有

Dim x = JsonConvert.DeserializeObject(Of Variants)(Response)MessageBox.Show(x.id)

我已经寻找了更多的解决方案,但其中大部分都是用 C# 编写的,它们让我感到困惑.如果有人可以帮助我,谢谢!

解决方案

除了您想要的部分之外,json 中还有 2 个其他事物".你可以想到你想要的部分:Container.variations.variant(N).您可以看到variations {}"将您想要的内容包装在 json 中,然后最外层的 { .. } 是被序列化的对象,这是另一个需要处理的类.所以:

公共类 VariationsContainer公共财产变体作为变体结束类公开课变化公共属性变体 As MyVariant()结束类公共类 MyVariant公共属性 ID 作为字符串公共财产 ATS 双倍公共属性 inStockDate 作为字符串结束类

Variant 是VB中的关键字,所以我在类中使用了MyVariant,属性名称必须与json中使用的一样,以避免使用JProperty.然后:

Dim jstr As String = 从任何地方Dim jData = JsonConvert.DeserializeObject(Of VariationsContainer)(jstr)Console.WriteLine(jData.variations.variants(0).id)

<块引用>

AQ5929_580

<小时>

如果你不想定义另外 2 个类,多 1-2 个步骤将摆脱它们和 obj.variations.Variants() 符号:

Dim jobj = JObject.Parse(jstr)Dim vars = JsonConvert.DeserializeObject(Of List(Of MyVariant))(jobj("variations")("variants").ToString)Console.WriteLine(vars(1).id)

<块引用>

AQ5929_590

这会导致 List(Of MyVariants) 使用 DeserializeObject(Of MyVariant()) 取回数组.

我很困惑在 JavaScriptSerializer 和 JSON.Net 之间做出选择 JavaScriptSerializer 类的 MSDN:

<块引用>

Json.NET 应该使用序列化和反序列化.[原文如此]

I want to fetch data from a website and populate some of the fields in my application's listview. I read few articles and I'm confused to choose between JavaScriptSerializer and JSON.Net. What is the best way to solve this?

Here is the JSON response from website

{
"variations": {
"variants": [
    {"id": "AQ5929_580",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ,

    {"id": "AQ5929_590",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ,

    {"id": "AQ5929_600",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ]
    }
}

I want only the values of id, inStock, ATS fields.

My VB .Net code so far,

<Serializable> _
Public Class Variants
    Private _id As String = Nothing
    Public Property id() As String
        Get
            Return _id
        End Get
        Set(value As String)
            _id = value
        End Set
    End Property

    Private _avStatusQuantity As Double
    Public Property avStatusQuantity() As Double
        Get
            Return _avStatusQuantity
        End Get
        Set(value As Double)
            _avStatusQuantity = value
        End Set
    End Property

    Private _avStatus As String = Nothing
    Public Property avStatus() As String
        Get
            Return _avStatus
        End Get
        Set(value As String)
            _avStatus = value
        End Set
    End Property

    Private _inStock As Boolean = False
    Public Property inStock() As Boolean
        Get
            Return _inStock
        End Get
        Set(value As Boolean)
            _inStock = value
        End Set
    End Property

    Private _ATS As String = Nothing
    Public Property ATS() As Double
        Get
            Return _ATS
        End Get
        Set(value As Double)
            _ATS = value
        End Set
    End Property

    Private _inStockDate As String = Nothing
    Public Property inStockDate() As String
        Get
            Return _inStockDate
        End Get
        Set(value As String)
            _inStockDate = value
        End Set
    End Property
End Class

<Serializable> _
Public Class Variations
    Private _Variants As Variants
    Public Property variants As Variants
        Get
            Return _Variants
        End Get
        Set(value As Variants)
            _Variants = value
        End Set
    End Property
End Class

When I use JSONConvert function for deserializing, result is always nothing

Dim x = JsonConvert.DeserializeObject(Of Variants)(Response)
MessageBox.Show(x.id)

I've looked for more solutions but most of them are in C# and they confuse me. If anyone can help me, Thanks!

解决方案

There are 2 other "things" in the json besides the part you want. You could think of the part you want being: Container.variations.variant(N). You can see the "variations {}" wrapping what you want in the json, then the outermost { .. } is the object which was serialized, which is another class to contend with. So:

Public Class VariationsContainer
    Public Property variations As Variations
End Class
Public Class Variations
    Public Property variants As MyVariant()
End Class

Public Class MyVariant
    Public Property id As String
    Public Property ATS As Double
    Public Property inStockDate As String
End Class

Variant is a keyword in VB, so I used MyVariant for the class, the property names have to be as used in the json to avoid the use of JProperty. Then:

Dim jstr As String = from whereever

Dim jData = JsonConvert.DeserializeObject(Of VariationsContainer)(jstr)
Console.WriteLine(jData.variations.variants(0).id)

AQ5929_580


If you dont want to define those 2 other classes, 1-2 more steps will get rid of them and the obj.variations.Variants() notation everywhere:

Dim jobj = JObject.Parse(jstr)
Dim vars = JsonConvert.DeserializeObject(Of List(Of MyVariant))(jobj("variations")("variants").ToString)
Console.WriteLine(vars(1).id)

AQ5929_590

This results in a List(Of MyVariants) use DeserializeObject(Of MyVariant()) to get an array back instead.

I'm confused to choose between JavaScriptSerializer and JSON.Net The first words on MSDN for JavaScriptSerializer Class:

Json.NET should be used serialization and deserialization.[sic]

这篇关于VB .Net 中的 JSON.Net 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆