VB.net 中 json.net 的简单工作示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21676708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple working Example of json.net in VB.net
提问by Great Big Al
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!
我有以下来自提供者的简化 JSON 字符串,自从我使用 Visual Studio 和 vb.Net 已经很长时间了,所以我很生疏!
{
"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"
}
}
}
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.
我想使用 JSON.Net 把它变成我可以使用的东西,我已经阅读了示例等,JSON.net 看起来像答案,但我无处可去。
My .Net code (Me.TextBox1.Text contains the JSON shown above)
我的 .Net 代码(Me.TextBox1.Text 包含上面显示的 JSON)
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
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.
有人可以解释为什么 obj.ID 总是以 0 结束,为什么我的类的其他属性都没有被填充,以及我需要做什么来解决这个问题,没有报告错误。
回答by sloth
Your class JSON_resultdoes not match your JSON string. Note how the object JSON_resultis going to represent is wrapped in another property named "Venue".
您的课程JSON_result与您的 JSON 字符串不匹配。请注意对象JSON_result将如何表示包含在另一个名为 的属性中"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...)
or change your JSON string to
或将您的 JSON 字符串更改为
{
"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"
}
}
or use e.g. a ContractResolverto parse the JSON string.
或使用例如 aContractResolver来解析 JSON 字符串。
回答by Dibu
Imports Newtonsoft.Json.Linq
Dim json As JObject = JObject.Parse(Me.TextBox1.Text)
MsgBox(json.SelectToken("Venue").SelectToken("ID"))
回答by Lucifer666
In Place of using this
代替使用这个
MsgBox(json.SelectToken("Venue").SelectToken("ID"))
You can also use
你也可以使用
MsgBox(json.SelectToken("Venue.ID"))

