Home > Software engineering >  Initialization conversion from C# to Visual Basic
Initialization conversion from C# to Visual Basic

Time:03-04

I am trying to convert the c# code below to VB. The code goes to a web page, and reads a series of text records. The content of the web response is posted below the code. This piece of code is supposed to read the query results.

//Post request example version 2.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v2/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { "CUUR0000SA0" }).ToArray(),
      startyear = "",
      endyear = "",
      catalog = true,
      calculations = true,
      annualaverage = true,
      registrationKey = "EnterRegistrationKeyHere"
  });
  //View the JSON output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}



The results for the series ID should look like:

      "series": [
        {
          "seriesID": "LAUCN040010000000005",
          "data": [
            {
              "year": "2013",
              "period": "M11",
              "periodName": "November",
              "value": "16393",
              "footnotes": [
                {
                  "code": "P",
                  "text": "Preliminary."
                }
              ]
            },
            {
              "year": "2013",
              "period": "M10",
              "periodName": "October",
              "value": "16536",
              "footnotes": [
                {
                 ...
                 }
              ]
            }
          ]

So far, I've got the code below.

        Dim streamWriter As System.IO.StreamWriter
        streamWriter = New System.IO.StreamWriter(httpWebRequest.GetRequestStream())
        Dim jS As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim SeriesPost() As Array
        SeriesPost = New SeriesPost() With {.seriesid = (New List() {"CUUR0000SA0"}).ToArray(),
                .startyear = "",
                .endyear = "",
                .catalog = True,
                .calculations = True,
                .annualaverage = True,
                .registrationKey = "EnterRegistrationKeyHere"
}
        Dim newJson As String
        newJson = jS.Serialize(loSeriesPost)

        '  //View the JSON output
        System.Diagnostics.Debug.WriteLine(newJson)
        streamWriter.Write(newJson)
        streamWriter.Flush()
        streamWriter.Close()

I can't figure out how to initialize SeriesPost and how to convert this initialization to VB.

Please advise.

Thank you. Alexander

CodePudding user response:

Try this:

Dim httpWebRequest = CType(WebRequest.Create("https://api.bls.gov/publicAPI/v2/timeseries/data/"), HttpWebRequest)
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"

    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        Dim jS = New JavaScriptSerializer()
        Dim newJson = jS.Serialize(New SeriesPost() With {
            .seriesid = (New List() From {
                "CUUR0000SA0"
            }).ToArray(),
            .startyear = "",
            .endyear = "",
            .catalog = True,
            .calculations = True,
            .annualaverage = True,
            .registrationKey = "EnterRegistrationKeyHere"
        })
        System.Diagnostics.Debug.WriteLine(newJson)
        streamWriter.Write(newJson)
        streamWriter.Flush()
        streamWriter.Close()
    End Using
  • Related