开发者

Converting the following C# sample to VB.NET

This is the sample in C#

using System.Web.Script.Serialization;

var jss = new JavaScriptSerializer();
var dict = jss.Deseri开发者_开发知识库alize<Dictionary<string,string>>(jsonText);

Console.WriteLine(dict["some_number"]); //outputs 108.541

For I keep getting errors when I try to use an online converter to convert it to vb.net. Does anyone know how to rewrite this in vb.net?

The error I get from http://www.developerfusion.com/tools/convert/csharp-to-vb/ is:

-- line 3 col 1: EOF expected


You have to put in valid C#.

I changed your input code to the following:

using System.Web.Script.Serialization;

public class Test {

   public void DoSomething() {

      var jss = new JavaScriptSerializer();
      var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);

      Console.WriteLine(dict["some_number"]); //outputs 108.541

   }
}

Then passed it through developer fusion to get:

Imports System.Web.Script.Serialization

Public Class Test

    Public Sub DoSomething()

        Dim jss = New JavaScriptSerializer()
        Dim dict = jss.Deserialize(Of Dictionary(Of String, String))(jsonText)

        Console.WriteLine(dict("some_number"))
        'outputs 108.541
    End Sub
End Class


Imports System.Web.Script.Serialization

Dim jss = New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of Dictionary(Of String, String))(jsonText)

Console.WriteLine(dict("some_number"))
'outputs 108.541


You would need some form of class in the file as currently your variables are being created outside of a class. Splitting the using apart as this should be at the top of the file I think that following should work:

Imports System.Web.Script.Serialization

The rest is:

Dim jss = New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of Dictionary(Of String,String))(jsonText)

Console.WriteLine(dict("some_number")) 'outputs 108.541

You also don't declare jsonText so I would not expect this code to run as it is. I have not tried to compile this but it looks ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜