简体   繁体   中英

use of code behind with serialized json using asp.net website

I generated an array from 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();

I Populated the object with that array in a for loop and serialized it afterwards

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 for the object is as followed:

[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;
}

so far so good.

my problem now is that i do have no clue how i can use the json in asp.net iterate througe it an use the attributes of the json object

I write the string into a TextBox from which i take the value

var sResultString = document.getElementById('<%= TextBox1.ClientID %>').value;
var obj = $.parseJSON(resultstring);

and try to parse it with $.parsejson(jsonString) and JSON.parse(jsonString) (its clear to me that this is dirty but i'm trying to survive there ) hope some of you can help me

var obj = eval($(“ [id $ ='TextBox1']”)。val());

Try the following JS code:

var sResultString = document.getElementById('<%= TextBox1.ClientID %>').value;
var obj = $.parseJSON(resultstring);
for(var i = 0; i < obj.length; i++){
  console.log(obj[i].Driver);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM