从 C# 到 Visual Basic 的初始化转换
Initialization conversion from C# to Visual Basic
我正在尝试将下面的 c# 代码转换为 VB。代码转到网页,并读取一系列文本记录。 Web 响应的内容发布在代码下方。这段代码应该读取查询结果。
//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();
}
系列 ID 的结果应如下所示:
"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": [
{
...
}
]
}
]
到目前为止,我得到了下面的代码。
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()
我不知道如何初始化 SeriesPost 以及如何将此初始化转换为 VB。
请指教
谢谢。
亚历山大
试试这个:
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
我正在尝试将下面的 c# 代码转换为 VB。代码转到网页,并读取一系列文本记录。 Web 响应的内容发布在代码下方。这段代码应该读取查询结果。
//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();
}
系列 ID 的结果应如下所示:
"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": [
{
...
}
]
}
]
到目前为止,我得到了下面的代码。
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()
我不知道如何初始化 SeriesPost 以及如何将此初始化转换为 VB。
请指教
谢谢。 亚历山大
试试这个:
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