I've recently created an array using a LINQ query:
var aTimeResultQuery =
(from fct in context.fct_testautomation
join dr in context.dim_driver on fct.driver_key equals dr.driver_key
join tc in context.dim_test_case on fct.test_case_key equals tc.test_case_key
join tr in context.dim_test_run on fct.test_run_key equals tr.test_run_key
where tr.test_suite_name == sSelectedTestSuite
orderby fct.fct_testautomation_key descending
select new
{
Duration = fct.Test_Duration,
Target_Duration = fct.Test_Duration_Target_Max,
Driver = dr.fused_driver,
Test_Suite = tr.test_suite_name,
Testcase = tc.test_case_type,
Test_Description = fct.test_description
})
.Take(int.Parse(txtTestrun.Text)).ToArray();
After populating the object with this array in a for loop, I proceeded to serialize it:
JavaScriptSerializer aSerializer = new JavaScriptSerializer();
for (int i = 0; i < aTimeResultQuery.Count(); i++)
{
aTimeGraph[i] = new TimeGraph();
aTimeGraph[i].Duration = aTimeResultQuery[i].Duration.ToString();
aTimeGraph[i].Target_Duration = aTimeResultQuery[i].Target_Duration.ToString();
aTimeGraph[i].Driver = aTimeResultQuery[i].Driver.ToString();
aTimeGraph[i].Test_Suite = aTimeResultQuery[i].Test_Suite.ToString();
aTimeGraph[i].Testcase = aTimeResultQuery[i].Testcase.ToString();
aTimeGraph[i].Test_Description = aTimeResultQuery[i].Test_Description.ToString();
//Serializer.WriteObject(aMemoryStream, aTimeGraph[i]);
}
string sJson = aSerializer.Serialize(aTimeGraph);
The class structure for the object is as follows:
[DataContract]
public class TimeGraph
{
[DataMember]
public string Duration;
[DataMember]
public string Target_Duration;
[DataMember]
public string Driver;
[DataMember]
public string Test_Suite;
[DataMember]
public string Testcase;
[DataMember]
public string Test_Description;
}
Now, I'm facing a challenge on how to effectively use the JSON data in ASP.NET and iterate through it to utilize the attributes of the JSON object.
I have attempted to write the JSON string into a TextBox and retrieve the value like this:
var sResultString = document.getElementById('<%= TextBox1.ClientID %>').value;
var obj = $.parseJSON(resultstring);
I've tried to parse it using $.parsejson(jsonString)
and JSON.parse(jsonString)
. While I am aware that this is not ideal, any help or suggestions would be greatly appreciated. Thank you!