6

I'm wondering how to create a objet class from json file or xml file ?

example :

I get this json file from webservice :

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

If I have no choice I will create manually my classes ... :-(

Thank in advance for your help

Eric

2 Answers 2

20

Since you're talking about XML and JSON files, I recommend you to install Web Tools 2012.2.

This adds a nice new feature to 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.

enter image description here

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. Amazing how many sites I've looked up about this and you're the first to mention that the solution is right here in my own kitchen!
You are a genius
2

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 :

JsonConvert.DeserializeObject(Of YourClass)(<JSON String>)

1 Comment

It should work too, but I'd rather Dominic Kexel' solution ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.