使用 JSON.NET VB.NET 解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21645678/
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
Parse JSON using JSON.NET VB.NET
提问by OCDan
I am trying to parse some JSON using JSON.NET; however, it is not something I am familiar with, and I am struggling to deserialize it.
我正在尝试使用 JSON.NET 解析一些 JSON;然而,这不是我熟悉的东西,我正在努力反序列化它。
This is the JSON:
这是 JSON:
{
"disclaimer": "use at own risk",
"license": "testing",
"timestamp": 1391770861,
"base": "USD",
"rates": {
"AED": 3.672839,
"AFN": 56.367,
"ALL": 103.5113,
"AMD": 412.35,
"ANG": 1.78894,
"AOA": 97.608324,
"ARS": 7.880804,
"AUD": 1.117779,
"AWG": 1.789825,
"AZN": 0.784133,
"BAM": 1.442736,
"BBD": 2
}
}
And here is my code currently:
这是我目前的代码:
Public Sub ParseJSON()
JSONResponse = String.Empty
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(LatestURL), HttpWebRequest)
request.ContentType = "application/json; charset=utf-8"
request.Accept = "application/json, text/javascript, */*"
request.Method = "POST"
Using writer As New StreamWriter(request.GetRequestStream())
writer.Write("{id : 'test'}")
End Using
Dim response As WebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Using StreamReader As New StreamReader(stream)
While Not StreamReader.EndOfStream
JSONResponse += StreamReader.ReadLine()
End While
End Using
Dim RateReply = JsonConvert.DeserializeObject(Of RateReply)(JSONResponse)
End Sub
Public Class RateReply
Public rates As IList(Of Rate)
Public base As String
Public timeStamp As Integer
Public license As String
Public disclaimer As String
Public Sub New()
rates = New List(Of Rate)()
End Sub
End Class
Public Class Rate
Public Currency As String
Public Rate As Decimal
End Class
The exception I am currently getting is:
我目前得到的例外是:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[KTMOM.Tests.Rate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.IList`1[KTMOM.Tests.Rate]”,因为该类型需要 JSON数组(例如 [1,2,3])以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为正常的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)可以从 JSON 对象反序列化的数组或列表)。还可以将 JsonObjectAttribute 添加到类型以强制它从 JSON 对象反序列化。
I believe this to be because the rates are not defined as an array in the JSON, however, I do not know how to resolve this.
我相信这是因为费率没有在 JSON 中定义为数组,但是,我不知道如何解决这个问题。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by har07
To resolve the problem you need to either alter JSON format to return array as the error message suggested, or if it isn't possible define another class to represent Rates:
要解决此问题,您需要更改 JSON 格式以按照错误消息的建议返回数组,或者如果无法定义另一个类来表示Rates:
Public Class Rates
Public AED As Double
.....
.....
Public BBD As Double
End Class
then define ratesfield in RateReplayclass as a Rates:
然后将类中的rates字段定义RateReplay为 a Rates:
Public rates As Rates
回答by Dibu
you can use
您可以使用
Dim Rates As Dictionary(Of String, Double)

