VB.net中json.net的简单工作示例 [英] Simple working Example of json.net in VB.net

查看:215
本文介绍了VB.net中json.net的简单工作示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从提供者那里得到了以下简化的JSON字符串,因为我使用Visual Studio和vb.Net已有很长时间了,所以我很生锈!

I have the following simplified JSON string from a provider, its been a long time since I used Visual Studio and vb.Net, so I'm very rusty!

{
"Venue": {
    "ID": 3145,
    "Name": "Big Venue, Clapton",
    "NameWithTown": "Big Venue, Clapton, London",
    "NameWithDestination": "Big Venue, Clapton, London",
    "ListingType": "A",
    "Address": {
        "Address1": "Clapton Raod",
        "Address2": "",
        "Town": "Clapton",
        "County": "Greater London",
        "Postcode": "PO1 1ST",
        "Country": "United Kingdom",
        "Region": "Europe"
    },
    "ResponseStatus": {
        "ErrorCode": "200",
        "Message": "OK"
    }
}
}

我想使用JSON.Net将其转换为我可以使用的东西,我已经阅读了示例等,并且JSON.net看起来像答案,但我无处可去.

I want to use JSON.Net to turn this in to something I can work with, I have read examples etc and JSON.net looks like the answer, but I'm getting no where.

我的.Net代码(Me.TextBox1.Text包含上面显示的JSON)

My .Net code (Me.TextBox1.Text contains the JSON shown above)

Imports Newtonsoft.Json

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim obj As JSON_result
    obj = JsonConvert.DeserializeObject(Of JSON_result)(Me.TextBox1.Text)

    MsgBox(obj.ID)
End Sub
End Class

Public Class JSON_result
    Public ID As Integer
    Public Name As String
    Public NameWithTown As String
    Public NameWithDestination As String
    Public ListingType As String
 End Class

有人可以解释一下为什么obj.ID总是以0结尾,为什么我的类的其他属性都没有填充,为什么我需要做些什么来解决此问题,所以没有错误报告.

Can someone explain why obj.ID always ends up as 0 please, and why none of the other properties of my class are populated and what I need to do to fix this, no errors are reported.

推荐答案

您的类JSON_result与您的JSON字符串不匹配.请注意,对象JSON_result将如何表示,该对象包装在另一个名为"Venue"的属性中.

Your class JSON_result does not match your JSON string. Note how the object JSON_result is going to represent is wrapped in another property named "Venue".

因此可以为此创建一个类,例如:

So either create a class for that, e.g.:

Public Class Container
    Public Venue As JSON_result
End Class

Public Class JSON_result
    Public ID As Integer
    Public Name As String
    Public NameWithTown As String
    Public NameWithDestination As String
    Public ListingType As String
End Class

Dim obj = JsonConvert.DeserializeObject(Of Container)(...your_json...)

或将JSON字符串更改为

or change your JSON string to

{
    "ID": 3145,
    "Name": "Big Venue, Clapton",
    "NameWithTown": "Big Venue, Clapton, London",
    "NameWithDestination": "Big Venue, Clapton, London",
    "ListingType": "A",
    "Address": {
        "Address1": "Clapton Raod",
        "Address2": "",
        "Town": "Clapton",
        "County": "Greater London",
        "Postcode": "PO1 1ST",
        "Country": "United Kingdom",
        "Region": "Europe"
    },
    "ResponseStatus": {
        "ErrorCode": "200",
        "Message": "OK"
    }
}

或使用例如ContractResolver来解析JSON字符串.

or use e.g. a ContractResolver to parse the JSON string.

这篇关于VB.net中json.net的简单工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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