我想知道如何从json文件或xml文件创建一个objet类?
示例:
我从webservice获取这个json文件:
{"nid":"3798","vid":"3788","type":"contact","language":"fr","title":"G","uid":"1","status":"1","created":"1374598689","changed":"1374598689","comment":"1","promote":"0","sticky":"0","tnid":"0","translate":"0"}
我想创建一个类:
Public Class Card
Public nid As Integer
Public vid As Integer
Public type As String
Public language As String
Public title As String
.
.
.
End Class
注意:
如果我别无选择,我将手动创建我的课程......: - (
提前感谢您的帮助
埃里克
答案 0 :(得分:13)
由于您在谈论XML和JSON文件,我建议您安装Web Tools 2012.2。
这为Visual Studio添加了一个不错的新功能:
将JSON粘贴为.NET类。使用此特殊粘贴命令将JSON粘贴到C#或VB.NET代码文件中,Visual Studio将自动生成从JSON推断的.NET类。
如果您有例如
{"nid":"3798","vid":"3788","type":"contact","language":"fr","title":"G","uid":"1","status":"1","created":"1374598689","changed":"1374598689","comment":"1","promote":"0","sticky":"0","tnid":"0","translate":"0"}
在剪贴板中,它将为您生成此课程:
Public Class Rootobject
Public Property nid As String
Public Property vid As String
Public Property type As String
Public Property language As String
Public Property title As String
Public Property uid As String
Public Property status As String
Public Property created As String
Public Property changed As String
Public Property comment As String
Public Property promote As String
Public Property sticky As String
Public Property tnid As String
Public Property translate As String
End Class
答案 1 :(得分:0)
您可以使用(http://json2csharp.com/)将json转换为csharp,然后使用http://www.developerfusion.com/tools/convert/csharp-to-vb/将csharp代码转换为vb.net。对于反序列化,您可以使用Newtonsoft.Json。您的反序列化代码将是:
JsonConvert.DeserializeObject(Of YourClass)(<JSON String>)