我有一個看起來像這樣的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