c # Json returning backslash

1

I'm new to web, I'm trying to return a json but it comes with bars like this:

{\"NomeUsuario\":\"TESTE\",\"TelefoneUsuario\":\"1111111111\"}

How do I make it look like this:

{"NomeUsuario":"TESTE","TelefoneUsuario":"1111111111"}

I have the class person:

[DataContract]
public class Pessoa
{
    [DataMember]
    public string Nome { get; set; }
    [DataMember]
    public string TelefoneUsuario { get; set; }
}

Method

 public string FiltradoPorPessoa(string Nome, string Variavel)
    {
        DataTable Dtconsulta = new DataTable();
        Pessoa retorno = null;

        Sql = " SELECT  NomeUsuario, TelefoneUsuario";
        Sql += " FROM Cadastro.DTUsuario";
        Sql += " WHERE NomeUsuario = '" + Nome.ToUpper() + "' AND SenhaUsuario = '" + GeraHash(Variavel.ToUpper()) + "'";

        DataTable dataTable = new DataTable();
        SqlCommand cmd = new SqlCommand(Sql, conn);
        conn.Open();

        adapter = new SqlDataAdapter(cmd);
        adapter.Fill(dataTable);

        if (dataTable.Rows.Count > 0)
        {
            Pessoa p = new Pessoa();

            DataTableReader DtConsultaReader = dataTable.CreateDataReader();
            while (DtConsultaReader.Read())
            { 
                p.Nome  = DtConsultaReader["NomeUsuario"].ToString();
                p.TelefoneUsuario = DtConsultaReader["TelefoneUsuario"].ToString();
            }
            DtConsultaReader.Close();
            conn.Close();

            var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(p);

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Pessoa));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            retorno = (Pessoa)ser.ReadObject(ms);  
        }
        return retorno;     
    }

And a method that returns a datatable, copy that to a datareader, what am I doing wrong, I can not return the string?

  

Can not implicitly convert type 'test15_04.Person' to 'string' in this way {"UserName": "TEST", "UserPhone": "1111111111"}

    
asked by anonymous 16.04.2018 / 13:30

1 answer

0

This bar is only visible in debug mode, if you put it to print the contents of this variable containing the JSON string will exit without the slash.

To work with JSON, I advise you to use Newtonsoft.Json, it is much more used and has much more document of how to work with it, for example to deserialize a JSON on an object is like this:

var objeto = JsonConvert.Deserialize<SeuObjeto>(stringJson);

and to get an object and generate a JSON string, it looks like this:

var json = JsonConvert.Serialize<TypeOf(SeuObjeto)>(seuObjeto);
    
17.04.2018 / 01:31