3

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.

1
  • Take a look at Json.NET Commented May 20, 2014 at 13:20

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.