c# Json returning backslash

Asked

Viewed 1,816 times

1

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

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

How do I make him 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, I copy this to a datareader, what I’m doing wrong, I can’t return the string?

Cannot implicitly Convert type 'teste15_04.Person' to 'string' in this way {"User name":"TEST","Phoneuser":"1111111111111"}

  • json.Replace("\", "");?

  • 2

    First, you understand the meaning of \" in string?

  • Second, you understand the meaning of " in the string?

  • I saw above that it is an escape character has to do with double quotes! but if using repace it removes all quotes from the string.

  • I don’t quite understand what you’re trying to do in your method, but at the end of it you try to return a objeto Pessoa and the method returns string

1 answer

1

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

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 take an object and generate a JSON string, it’s like this:

var json = JsonConvert.Serialize<TypeOf(SeuObjeto)>(seuObjeto);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.