1
I’m returning a bank query for a DataTable
and using it to Json
.
I use the following method to Serialize my Datatable for Json.
public static string ConvertDataTabletoString(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;
foreach (System.Data.DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (System.Data.DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
And I have the following result:
[{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[96]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[95.51]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[89.7]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[97.05]"},{"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Conventional","date":"[86.86]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[90]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[47.35]"}]
My code is right, you can see it here on JSFIELD.
Finding out the only thing wrong would be on the property data
, that instead of "data":[86.86]
is "data":"[86.86]"
. Is there any way to control it?
Makes
var data = JSON.parse(resultadoDoServidor);
. This should be enough to convert the data.– Sergio
@Sergio Chega appears the Graph no more plots the data.
– Marconi
You can show how you are using the data in JS?
– Sergio
@Sergio Can yes, I’m trying here.
– Marconi
But in this jsFiddle the
series: [{
it’s already right... how you’re importing it from C#?– Sergio
@Sergio I removed in hand the quotation marks of the date value. C# comes as in the above result.
– Marconi
But are you printing that right inside the JS? Can you put that part of the code here in the question? the part where you compile the JS with the data inside...
– Sergio
@Sergio The function of the friend below works, only need to convert deserialize the object to Array.
– Marconi
Put pf the code that prints on the JS page.
– Sergio
@Sergio is the result above. However I used var data = JSON.parse(resultDoServitor);
– Marconi
Let’s go continue this discussion in chat.
– Sergio
@Sergio saw the result I showed?
– Marconi
I’m at work and with little time. I saw the answer yes. Do you have it online? I think the JSON parse is going wrong. I wanted to see what the server sends in Veloper tools. But if you pass this server response without using
type: 'json'
in AJAX, you could parse inside the jQuery Success function... But I need to test. The @Malaba solution works, but it’s patch. The object should already be clean, without needing a.map()
and much less with Regexp to clear quotes that involve an array...– Sergio
@Sergio Entendi.
– Marconi