如何从json文件(或xml文件)创建vb.net对象类

时间:2014-03-20 09:42:40

标签: xml json vb.net

我想知道如何从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

注意:

  • 我的问题不是如何在vb.net中序列化/反序列化json objet?
  • 我的xml文件没有XSD,为什么更难?
  • 我的代码是用VB.Net编写的,不是用C#编写的。我发现很多网站将json转换为c#(http://json2csharp.com/),但没有json转换为vb.net

如果我别无选择,我将手动创建我的课程......: - (

提前感谢您的帮助

埃里克

2 个答案:

答案 0 :(得分:13)

由于您在谈论XML和JSON文件,我建议您安装Web Tools 2012.2

这为Visual Studio添加了一个不错的新功能:

  

将JSON粘贴为.NET类。使用此特殊粘贴命令将JSON粘贴到C#或VB.NET代码文件中,Visual Studio将自动生成从JSON推断的.NET类。

enter image description here

如果您有例如

{"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>)