Home > Software engineering >  How to use Newtonsoft.Json to output a json file in vb.net
How to use Newtonsoft.Json to output a json file in vb.net

Time:01-03

The c# sample code I saw in the Newtonsoft.Json document is like this
But I don’t quite convert it into vb.net code

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using (JsonWriter writer = new JsonTextWriter(sw))
{
    writer.Formatting = Formatting.Indented;

    writer.WriteStartObject();
    writer.WritePropertyName("CPU");
    writer.WriteValue("Intel");
    writer.WritePropertyName("PSU");
    writer.WriteValue("500W");
    writer.WritePropertyName("Drives");
    writer.WriteStartArray();
    writer.WriteValue("DVD read/writer");
    writer.WriteComment("(broken)");
    writer.WriteValue("500 gigabyte hard drive");
    writer.WriteValue("200 gigabyte hard drive");
    writer.WriteEnd();
    writer.WriteEndObject();
}

// {
//   "CPU": "Intel",
//   "PSU": "500W",
//   "Drives": [
//     "DVD read/writer"
//     /*(broken)*/,
//     "500 gigabyte hard drive",
//     "200 gigabyte hard drive"
//   ]
// }

How can I transcribe him in vb.net?
I have tried to use some C# to vb.net code tools, but they are all wrong.

CodePudding user response:

You are not showing the whole code, so it is difficult to help you further. To install and use Newtonsoft.json do the following: 1: On your Visual Studio Project menu open your NuGet package manager, and install “Newtonsoft.Json” into your project.

2: Type the following VB code at the top of the file you are trying to use the package:

Imports Newtonsoft.Json

The code translate as follow:

Imports System.IO
Imports System.Text
Imports Newtonsoft.Json
Public Class YourClassName
    Private Sub subname() 
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)

        Using writer As JsonWriter = New JsonTextWriter(sw)

            writer.Formatting = Formatting.Indented

            writer.WriteStartObject()
            writer.WritePropertyName("CPU")
            writer.WriteValue("Intel")
            writer.WritePropertyName("PSU")
            writer.WriteValue("500W")
            writer.WritePropertyName("Drives")
            writer.WriteStartArray()
            writer.WriteValue("DVD read/writer")
            writer.WriteComment("(broken)")
            writer.WriteValue("500 gigabyte hard drive")
            writer.WriteValue("200 gigabyte hard drive")
            writer.WriteEnd()
            writer.WriteEndObject()
        End Using

        ' {
        '  "CPU": "Intel",
        '   "PSU": "500W",
        '   "Drives": [
        '     "DVD read/writer"
        '     /*(broken)*/,
        '     "500 gigabyte hard drive",
        '     "200 gigabyte hard drive"
        '   ]
        ' }
    End Sub

Notes: in C# this convert to: using Newtonsoft.Json; Then the rest of your code... hope this helps.

// translate to ' for comments.

: are not used in VB.net

The following page is very useful to convert code from C# to vb.net and vice-versa: https://converter.telerik.com/

  • Related