I donβt have VB in my box, but if you want to use the JavaScriptSerializer class, your project should focus on the .NET Framework 3.5 or the .NET Framework 4.0 (not the .NET Framework 3.5 Client Profile, not the .NET Framework 4.0 client profile ), Add System.Web.Extensions.dll to the links.
Example in C #:
public String Index() { Object[] myArray = new Object[3]; myArray[0] = new { col1 = "foo", col2 = "bar" }; myArray[1] = new { col1 = "fizz", col2 = "buzz" }; myArray[2] = new { col1 = "fizz", col2 = "buzz" }; JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(myArray); }
The output of this function:
[{"col1":"foo","col2":"bar"},{"col1":"fizz","col2":"buzz"},{"col1":"fizz","col2":"buzz"}]
It is easy to convert to VB.
Update: VB Version:
Function get_json() As String Dim myArray(2) As Object myArray(0) = New With {Key .col1 = "foo", .col2 = "bar"} myArray(1) = New With {Key .col1 = "fizz", .col2 = "buzz"} myArray(2) = New With {Key .col1 = "fizz", .col2 = "buzz"} Dim serializer As New JavaScriptSerializer() Return serializer.Serialize(myArray) End Function
The same conclusion, just remember to import System.Web.Script.Serialization .
source share