VB.net使用JSON.net反序列化JSON
两周以来我一直在寻找解决问题的方法。 我想用JSON.NET反序列化JSON,但现在 我创建了一个类,但当我反序列化对象时,它什么也没有保留 这里是JSON:VB.net使用JSON.net反序列化JSON,json,vb.net,json.net,deserialization,Json,Vb.net,Json.net,Deserialization,两周以来我一直在寻找解决问题的方法。 我想用JSON.NET反序列化JSON,但现在 我创建了一个类,但当我反序列化对象时,它什么也没有保留 这里是JSON: {"plannifReponse": {"@competence":"Abonnement","plannifDonnees": {"entry": [ {"key":"2013-8-11T00:00","value": {"creneaux": [ {"@jour":"2013-8-11T00:00","@heure":"09","@m
{"plannifReponse":
{"@competence":"Abonnement","plannifDonnees":
{"entry":
[
{"key":"2013-8-11T00:00","value":
{"creneaux":
[
{"@jour":"2013-8-11T00:00","@heure":"09","@minute":"30","nombreRessources":10},
{"@jour":"2013-8-11T00:00","@heure":"10","@minute":"30","nombreRessources":2},
{"@jour":"2013-8-11T00:00","@heure":"17","@minute":"30","nombreRessources":5},
{"@jour":"2013-8-11T00:00","@heure":"20","@minute":"30","nombreRessources":5},
{"@jour":"2013-8-11T00:00","@heure":"21","@minute":"00","nombreRessources":16}
]
}
},
{"key":"2013-7-30T00:00","value":
{"creneaux":
[{"@jour":"2013-7-30T00:00","@heure":"12","@minute":"00","nombreRessources":4},{"@jour":"2013-7-30T00:00","@heure":"12","@minute":"15","nombreRessources":10},{"@jour":"2013-7-30T00:00","@heure":"12","@minute":"30","nombreRessources":3},{"@jour":"2013-7-30T00:00","@heure":"14","@minute":"00","nombreRessources":8},{"@jour":"2013-7-30T00:00","@heure":"18","@minute":"30","nombreRessources":10}]}}]}}}
为此,我与该课程一起翻译:
Public Class plannifReponse
Public competence As String
Public plannifDonnees As Dictionary(Of String, ListCreneaux)
End Class
Public Class ListCreneaux
Public listCreneaux() As Creneau
End Class
Public Class Creneau
Public jour As String
Public heure As String
Public minute As String
Public nombreRessources As Integer
Public Sub New(ByVal _jour, ByVal _heure, ByVal _minute, ByVal _nombreRessources)
jour = _jour
heure = _heure
minute = _minute
nombreRessources = _nombreRessources
End Sub
End Class
以及守则:
Dim prev As plannifReponse = JsonConvert.DeserializeObject(Of plannifReponse)(My_dispos)
但它不工作,没有错误消息,但prev什么也不保留
为了获得帮助,这里的源对象用于序列化它是在Java上的
public class OutputPlannif {
private String competence;
private HashMap<String, ListCreneaux> plannifDonnees;
}
public class ListCreneaux {
private ArrayList<Creneau> listCrenaux;
}
public class Creneau {
private String jour;
private String heure;
private String minute;
private int nombreRessources;
}
如果有人有主意。。。
谢谢
Matt您应该创建一系列类来映射要反序列化的JSON。有一些工具可以帮你做到这一点。或者,您也可以手工操作,每次使用一名成员,结果如下所示:
Public Class StackOverflow_17956746
Public Class OutputPlannif
<JsonProperty("plannifReponse")> _
Public PlannifReponse As PlannifReponse
End Class
Public Class PlannifReponse
<JsonProperty("@competence")> _
Public Competence As String
<JsonProperty("plannifDonnees")> _
Public PlannifDonnees As PlannifDonnees
End Class
Public Class PlannifDonnees
<JsonProperty("entry")> _
Public Entries As List(Of Entry)
End Class
Public Class Entry
<JsonProperty("key")> _
Public Key As String
<JsonProperty("value")> _
Public Value As Value
End Class
Public Class Value
<JsonProperty("creneaux")> _
Public ListCreneaux As List(Of Creneau)
End Class
Public Class Creneau
<JsonProperty("@jour")> _
Public Jour As String
<JsonProperty("@heure")> _
Public Heure As String
<JsonProperty("@minute")> _
Public Minute As String
<JsonProperty("nomberRessources")> _
Public NombreRessources As Integer
End Class
Const JSON As String = "{" & vbCrLf & _
" ""plannifReponse"":" & vbCrLf & _
"{""@competence"":""Abonnement"",""plannifDonnees"":" & vbCrLf & _
"{""entry"":" & vbCrLf & _
"[" & vbCrLf & _
"{""key"":""2013-8-11T00:00"",""value"":" & vbCrLf & _
"{""creneaux"":" & vbCrLf & _
"[" & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""09"",""@minute"":""30"",""nombreRessources"":10}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""10"",""@minute"":""30"",""nombreRessources"":2}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""17"",""@minute"":""30"",""nombreRessources"":5}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""20"",""@minute"":""30"",""nombreRessources"":5}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""21"",""@minute"":""00"",""nombreRessources"":16}" & vbCrLf & _
"]" & vbCrLf & _
"}" & vbCrLf & _
"}," & vbCrLf & _
"{""key"":""2013-7-30T00:00"",""value"":" & vbCrLf & _
"{""creneaux"":" & vbCrLf & _
"[{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""00"",""nombreRessources"":4},{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""15"",""nombreRessources"":10},{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""30"",""nombreRessources"":3},{""@jour"":""2013-7-30T00:00"",""@heure"":""14"",""@minute"":""00"",""nombreRessources"":8},{""@jour"":""2013-7-30T00:00"",""@heure"":""18"",""@minute"":""30"",""nombreRessources"":10}]}}]}}}"
Public Shared Sub Test()
Dim output As OutputPlannif
output = JsonConvert.DeserializeObject(Of OutputPlannif)(JSON)
Console.WriteLine(output)
End Sub
End Class
您应该创建一系列类来映射要反序列化的JSON。有一些工具可以帮你做到这一点。或者,您也可以手工操作,每次使用一名成员,结果如下所示:
Public Class StackOverflow_17956746
Public Class OutputPlannif
<JsonProperty("plannifReponse")> _
Public PlannifReponse As PlannifReponse
End Class
Public Class PlannifReponse
<JsonProperty("@competence")> _
Public Competence As String
<JsonProperty("plannifDonnees")> _
Public PlannifDonnees As PlannifDonnees
End Class
Public Class PlannifDonnees
<JsonProperty("entry")> _
Public Entries As List(Of Entry)
End Class
Public Class Entry
<JsonProperty("key")> _
Public Key As String
<JsonProperty("value")> _
Public Value As Value
End Class
Public Class Value
<JsonProperty("creneaux")> _
Public ListCreneaux As List(Of Creneau)
End Class
Public Class Creneau
<JsonProperty("@jour")> _
Public Jour As String
<JsonProperty("@heure")> _
Public Heure As String
<JsonProperty("@minute")> _
Public Minute As String
<JsonProperty("nomberRessources")> _
Public NombreRessources As Integer
End Class
Const JSON As String = "{" & vbCrLf & _
" ""plannifReponse"":" & vbCrLf & _
"{""@competence"":""Abonnement"",""plannifDonnees"":" & vbCrLf & _
"{""entry"":" & vbCrLf & _
"[" & vbCrLf & _
"{""key"":""2013-8-11T00:00"",""value"":" & vbCrLf & _
"{""creneaux"":" & vbCrLf & _
"[" & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""09"",""@minute"":""30"",""nombreRessources"":10}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""10"",""@minute"":""30"",""nombreRessources"":2}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""17"",""@minute"":""30"",""nombreRessources"":5}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""20"",""@minute"":""30"",""nombreRessources"":5}," & vbCrLf & _
"{""@jour"":""2013-8-11T00:00"",""@heure"":""21"",""@minute"":""00"",""nombreRessources"":16}" & vbCrLf & _
"]" & vbCrLf & _
"}" & vbCrLf & _
"}," & vbCrLf & _
"{""key"":""2013-7-30T00:00"",""value"":" & vbCrLf & _
"{""creneaux"":" & vbCrLf & _
"[{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""00"",""nombreRessources"":4},{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""15"",""nombreRessources"":10},{""@jour"":""2013-7-30T00:00"",""@heure"":""12"",""@minute"":""30"",""nombreRessources"":3},{""@jour"":""2013-7-30T00:00"",""@heure"":""14"",""@minute"":""00"",""nombreRessources"":8},{""@jour"":""2013-7-30T00:00"",""@heure"":""18"",""@minute"":""30"",""nombreRessources"":10}]}}]}}}"
Public Shared Sub Test()
Dim output As OutputPlannif
output = JsonConvert.DeserializeObject(Of OutputPlannif)(JSON)
Console.WriteLine(output)
End Sub
End Class
谢谢你的帮助。那很好用。我从未见过这个网站,但它真的很有用。谢谢你的帮助。那很好用。我从未见过这个网站,但它确实很有用。