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("\", "");
?– Costamilam
First, you understand the meaning of
\"
in string?– Ricardo Pontual
Second, you understand the meaning of " in the string?
– Rovann Linhalis
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.
– InovationApp App
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 returnsstring
– Barbetta