How to create JSON data with .NET?

At the moment, I am building json data as follows:

<%@ Page Language="VB" Debug="True" EnableViewState="false" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.IO" %> <script runat="server"> Dim objSQLConnection As SqlConnection Dim objSQLCommand As SqlCommand Dim objSQLDataReader As SqlDataReader Dim objJSONStringBuilder As StringBuilder Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Response.Clear() Response.ContentType = "application/json" Response.Write(get_json()) Response.End() End Sub Function get_json() As String objJSONStringBuilder = New StringBuilder() objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString")) objSQLCommand = New SqlCommand("sql query goes here", objSQLConnection) objJSONStringBuilder.Append("[") objSQLCommand.Connection.Open() objSQLDataReader = objSQLCommand.ExecuteReader() While objSQLDataReader.Read() objJSONStringBuilder.Append("{") objJSONStringBuilder.Append("""col1""") objJSONStringBuilder.Append(":") objJSONStringBuilder.Append("""" & objSQLDataReader("col1") & """") objJSONStringBuilder.Append(",") objJSONStringBuilder.Append("""col2""") objJSONStringBuilder.Append(":") objJSONStringBuilder.Append("""" & objSQLDataReader("col2") & """") objJSONStringBuilder.Append(",") objJSONStringBuilder.Append("""col3""") objJSONStringBuilder.Append(":") objJSONStringBuilder.Append("""" & objSQLDataReader("col3") & """") objJSONStringBuilder.Append("},") End While objSQLDataReader.Close() objSQLCommand.Connection.Close() objJSONStringBuilder.Remove(objJSONStringBuilder.Length - 1, 1) objJSONStringBuilder.Append("]") Return objJSONStringBuilder.ToString End Function </script> 

Is this the preferred method of creating JSON data using .NET?

I think I should use asmx web services with arrays converted to json? But all the examples that I saw are in C #.

+6
source share
5 answers

Normally, with ASP.NET, you do not have to do anything to serialize JSON. Just make your request with the correct type, return it correctly, and ASP.NET will serialize to JSON for you. It can also be deserialized.

+8
source

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 .

+8
source

You can try Json.NET . This is a very popular library for processing json objects on the .net platform.

+2
source

JavaScriptSerializer Class

The JavaScriptSerializer class is used by the internal asynchronous communication layer to serialize and deserialize the data that is transferred between the browser and the web server. You cannot access this instance of serializer. However, this class provides a public API. Therefore, you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods. To serialize and deserialize types that are not supported by JavaScriptSerializer, implement custom converters using the JavaScriptConverter class. Then register the transducers using the RegisterConverters method.

0
source

Source: https://habr.com/ru/post/891205/


All Articles