2013-06-27 63 views
1

我生成從LINQ查詢的數組:使用的代碼後面用序列JSON使用asp.net網站

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(); 

我填充在該陣列,用於循環目的和序列化它事後

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); 

該對象的類如下:

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

到目前爲止好。現在

我的問題是,我不知道我怎麼可以使用JSON在asp.net 迭代througe它的使用JSON對象的屬性

我寫串入一個TextBox從中我取值

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

,並嘗試與$.parsejson(jsonString)JSON.parse(jsonString)解析它(它的清楚,我認爲這是骯髒的,但我試圖在那裏生存)希望你們中的一些可以幫助我

回答

0

VAR OBJ = eval ($( 「[ID $ = 'TextBox1中']」)VAL());

+0

我用你的代碼行。如果我現在'alert(obj)'我的對象它說[對象對象],但如果我像它'alert(obj.Driver)'它說undefined爲什麼我錯過了什麼? – Zulummar

+1

試試這個,alert(obj [0] .Driver) – printf

0

試試下面的JS代碼:

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