2015-09-16 74 views
0

因此,我使用JSON序列化了C#中的對象列表以發送給JS。該列表似乎到達瀏覽器,但我無法弄清楚如何正確使用它。它是有道理的,什麼到達是一個字符串,但它似乎實際上是一個數組...我不知道,我不知道如何使用它。將JSON字符串/數組解析爲JS對象

這裏是我的JS

var data; 
function testFunc() { 
    d3.select("#stuff").append("h2").text(data[0].source); 
} 

當我送一個對象上面的JS正確打印出值。下面是C#

protected void btnTest_Click(object sender, EventArgs e) 
     { 
      string json = JsonConvert.SerializeObject(new testClass(66,77)); 
      ClientScript.RegisterArrayDeclaration("data", json); 
      ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "id", "testFunc()", true); 
     } 

當我看到瀏覽器的調試器我看到上面時執行下面這條線:

var data = new Array({"target":66,"source":77}); 

這是什麼讓我打印值77在JS以上

令人討厭的是我想發送一個完全相同的對象列表。所以我用下面的C#

List<TestGraph.Models.testClass> L = new List<TestGraph.Models.testClass>() 
private List<testClass> fill() 
     { 

      for (int i = 0; i < 10; i++) 
      { 
       L.Add(new testClass(i, i+1)); 
      } 
      return L; 
     } 
     protected void btnTest_Click(object sender, EventArgs e) 
     { 
      fill(); 
      string json = JsonConvert.SerializeObject(L); 
      ClientScript.RegisterArrayDeclaration("data", json); 
      ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "id", "testFunc()", true); 
     } 

當我使用相同的JS也不會打印任何東西了,不過名單越來越給JS,因爲我看到下面當我看到瀏覽器的調試器:

var data = new Array([{"target":0,"source":1},{"target":1,"source":2},{"target":2,"source":3},{"target":3,"source":4},{"target":4,"source":5},{"target":5,"source":6},{"target":6,"source":7},{"target":7,"source":8},{"target":8,"source":9},{"target":9,"source":10}]); 

因此,由於數據列表在瀏覽器中,我該如何使用它?

PS不是真的有必要,但這裏是我的TestClass如果有人好奇

public class testClass 
    { 
     public int target { get; set; } 
     public int source { get; set; } 
     public testClass(int t, int s) 
     { 
      target = t; 
      source = s; 
     } 
     public testClass() 
     { 

     } 
    } 

編輯

對於那些暗示我一直在使用JSON嘗試。解析(數據)

我用這個:

var data; 
var data2 = JSON.parse(data); 
function testFunc() { 
    d3.select("#stuff").append("h2").text(data2[1].source); 
} 

EDIT

因此,當我通過C#行步驟:

JsonConvert.SerializeObject(L); 

提出下面的字符串爲JSON var:

"[{\"target\":0,\"source\":1},{\"target\":1,\"source\":2},{\"target\":2,\"source\":3},{\"target\":3,\"source\":4},{\"target\":4,\"source\":5},{\"target\":5,\"source\":6},{\"target\":6,\"source\":7},{\"target\":7,\"source\":8},{\"target\":8,\"source\":9},{\"target\":9,\"source\":10}]" 

那麼在理論上,當我打電話:

ClientScript.RegisterArrayDeclaration("data", json); 

應該把上面的串入「數據」無功在js但是當我做就可以了警報就這樣:

var data; 
function testFunc() { 
    alert(data); 
} 

什麼是出現

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] 

我也嘗試過其他方法在我的C#:

protected void btnTest_Click(object sender, EventArgs e) 
     { 
      fill(); 
      string json = JsonConvert.SerializeObject(L); 
      Response.Write(string.Concat("<input id='data' type='hidden' value='", json, "' />")); 
      ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "id", "testFunc()", true); 
     } 

這需要以下更改JS

var field = document.getElementById('data'); 
var data = JSON.parse(field.value); 
function testFunc() { 
    alert(data); 
} 

當我嘗試這種新方法,我得到和以前一樣:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] 

而且我做了現場VAR進行第二次測試從上面的方法,並得到

[object HTMLInputElement] 
+0

請添加樣品JSON –

+0

'VAR數據=新陣列([{ 「目標」:0, 「源極」:1},{ 「目標」:1, 「源」 :2},{ 「目標」:2, 「源極」:3},{ 「目標」:3, 「源極」:4},{ 「目標」:4, 「源極」:5},{ 「目標」 :5中, 「源」:6},{ 「目標」:6中, 「源」:7},{ 「目標」:7, 「源極」:8},{ 「目標」:8中, 「源」:9 },{「target」:9,「source」:10}]);'是另一個無意義數組中的對象數組,因爲var ary = []'與var ary = new Array() 。 – PHPglue

+0

@PHPglue我可以看到,但我沒有寫。它就在我的瀏覽器調試器中。這就是發送到瀏覽器的內容。 – nhoughto

回答

2

只使用JSON.parse()

例如:

var parsed = JSON.parse('[{"target":0,"source":1},{"target":1,"source":2},{"target":2,"source":3},{"target":3,"source":4},{"target":4,"source":5},{"target":5,"source":6},{"target":6,"source":7},{"target":7,"source":8},{"target":8,"source":9},{"target":9,"source":10}]'); 
    console.log(parsed[1].target); 
相關問題