Convert vb.net String to Json Object?

json, vb.net

Solution

I guess there are two ways to do it. Since this is a simple string, you can just brute force it:

 Dim jsonMsg = "{""msg"":""" & msg & """}"

A more sophisticated way is put it into a class and serialize it.

Public Class MyMessage
  Public Property Msg As String
  Public Sub New(myMsg as String)
    Msg = myMsg
  End Sub
End Class

Dim myMsg As New MyMessage(msg)
Dim serializer as new JavaScriptSerializer
Dim jsonMsg = serializer.Serialize(myMsg)

You need a reference to System.Runtime.CompilerServices.

Problem

I am new in vb.net, I am developing a webservice. I want to send a response in a json object. I have only one string in response. ``` public string GetUser(String IMEI) { string msg = ""; string SQL1 = "Select Email from [Customer] where [Vehicle]='" + IMEI + "'"; DataTable dt = dbcom.GetDataTable(SQL1); if (dt.Rows.Count > 0) { msg = dt.Rows[0]["Email"].ToString(); //CV(username, IMEI); //vehiclechk(IMEI); } return msg; } ``` This send xml string. How we convert `msg` string in to json.

Original source