i Daten wie folgt aus datierbaren von SQL lokalen Server empfange wie folgt aussiehtentfernen Doppel Zitat von Json von Datatable C# erhalten
„{ 'USER_NAME': 'adam2', 'E-MAIL':‘ [email protected]‘, 'FIRST_NAME': 'Adam Anas'}“
public class UsersController : ApiController
{
public string openconn(string strQuery)
{
SqlConnection conn = new SqlConnection("Data Source=SAMIB-20042\\SQLEXPRESS;Initial Catalog=SSOwebAPI;Integrated Security=True");
conn.Open();
// Create a command to extract the required data and assign it the connection string
SqlCommand cmd = new SqlCommand(strQuery, conn);
cmd.CommandType = CommandType.Text;
// Create a DataAdapter to run the command and fill the DataTable
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return GetJson(dt);
}
public string GetJson(DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows =
new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim(), dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
public string GetName(string Username)
{
string strQuery = @"USE SSOwebAPI;select USER_NAME,EMAIL,FIRST_NAME,LAST_NAME,FIRST_NAME_AR,LAST_NAME_AR,MOBILE_NUMBER,USER_ACCOUNT_ID,CREATION_DATE,UAE_ID_NUMBER,PASSPORT_NO,LICENSE_NUMBER,NAME_EN,NAME_AR,CASE_PARTY_ID FROM [dbo].[Table] WHERE USER_NAME =" + "'" + Username + "'";
string resulw = openconn(strQuery);
string outputjson = resulw.Replace("[", string.Empty).Replace("]", string.Empty);
string outputjsona = outputjson.Replace("\"", string.Empty).Replace("'", string.Empty);
return outputjsona;
}
}
}
I r versucht eMove das doppelte Anführungszeichen am Anfang aber nicht, ich habe auch versucht, json Formatierer
// GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
// new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
aber das doppelte Anführungszeichen am Anfang machen kann nicht entfernt wird mir
bitte hilft ich stecken bin, weil ich json nicht bestätigen können, so ich kann es in meinem android-Handy verwenden
danke
Keine Spam-Tags. – Olaf
Wenn Sie das '' 'entfernen, ist Ihre Zeichenfolge keine gültige JSON-Datei. Versuchen Sie Folgendes:' jsonString.Replace ("\" "," "). Replace (" '"," \ ""); ' – uTeisT