我有一个看起来像这样的JSON对象。将自定义JSON对象转换为VB.NET对象
{
"Errors":{
"err1":[
//* Array of err1 objects
],
"err2":[
//* Array of err2 objects
]
}
}
我使用此对象来报告在PHP页面的请求中发现的错误。
我想那个对象转换为vb.Net对象声明基本上是这样的:
Public Class WebErrContainer
Public Errors as List(Of IWebError)
End Class
Public Class Err1
Implements IWebError
End Class
Public Class Err2
Implements IWebError
End Class
Public Interface IWebError
End Interface
我不知道如果我的实现是不够好,我是比较新的VB.NET这样我的OOP技能现在有点低。
就是这样......我想我已经解释了情况,以及我可以。
如果您需要更多信息,我会给它。
在此先感谢您提供的任何帮助。谢谢。
PD:我目前使用Newtonsoft的JSON.Net库。随意推荐一些其他库或方法来处理VB.NET中的JSON。
我做这个解决情况:
Public Sub New(ByVal jsonText As String)
Dim jObject As JObject = jObject.Parse(jsonText)
Dim jErrors As JToken = jObject("Errors")
Dim jS = New JsonSerializer()
' This is the "temporal" dictionary that stores the errors like they are
' stored in JSON
Dim jsErrs As New Dictionary(Of String, List(Of Object))
' List that is going to be passed to the "Errors" field in the class
Dim lErrors As New List(Of IWebError)
jsErrs = jS.Deserialize(New JTokenReader(jErrors), jsErrs .GetType)
If Not jsErrs Is Nothing Then
For Each errType As KeyValuePair(Of String, List(Of Object)) In jsErrs
For Each Err As Object In errType.Value
lErrors .Add(jS.Deserialize(New JTokenReader(Err), Type.GetType(errType.Key)))
Next
Next
End If
Me.Errors = lErrors
End Sub
我添加了一个解决方案,我出来了...请随时报告代码中发现的任何错误。 谢谢你的答案svick和Jayesh ... – saggio09 2012-04-05 17:04:59