如何从 json 文件(或 xml 文件)创建 vb.net 对象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22528884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create vb.net object class from json file (or xml file)
提问by Cooxkie
I'm wondering how to create a objet class from json file or xml file ?
我想知道如何从 json 文件或 xml 文件创建一个对象类?
example :
例子 :
I get this json file from webservice :
我从 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"}
I would like to create a class like :
我想创建一个类,如:
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
NB :
注意:
- My question is not how to serialize / deserialize json objet in vb.net ?
- My xml file doesn't have XSD that why is more difficult
- My code is written in VB.Net not in C#. I found many website which convert json to c# (http://json2csharp.com/), but nothing json to vb.net
- 我的问题不是如何在 vb.net 中序列化/反序列化 json 对象?
- 我的 xml 文件没有 XSD,为什么更难
- 我的代码是用 VB.Net 编写的,而不是用 C# 编写的。我发现了许多将 json 转换为 c#(http://json2csharp.com/)的网站,但没有任何 json 转换为 vb.net
If I have no choice I will create manually my classes ... :-(
如果我别无选择,我将手动创建我的课程...... :-(
Thank in advance for your help
预先感谢您的帮助
Eric
埃里克
回答by sloth
Since you're talking about XML and JSON files, I recommend you to install Web Tools 2012.2.
由于您在谈论 XML 和 JSON 文件,因此我建议您安装Web Tools 2012.2。
This adds a nice new feature to Visual Studio:
这为 Visual Studio 添加了一个不错的新功能:
Paste JSON as a .NET class. Using this Special Paste command to paste JSON into a C# or VB.NET code file, and Visual Studio will automatically generate .NET classes inferred from the JSON.
将 JSON 粘贴为 .NET 类。使用此特殊粘贴命令将 JSON 粘贴到 C# 或 VB.NET 代码文件中,Visual Studio 将自动生成从 JSON 推断出的 .NET 类。


If you have e.g.
如果你有例如
{"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"}
in your clipboard, it will generate this class for you:
在您的剪贴板中,它将为您生成此类:
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
回答by Sargis Koshkaryan
You can convert json to csharp using (http://json2csharp.com/) and then convert csharp code to vb.net using http://www.developerfusion.com/tools/convert/csharp-to-vb/. For deserialization you can use Newtonsoft.Json. Your deserialization code will be :
您可以使用 ( 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>)

