所以我使用JSON来序列化C#中的对象列表以发送给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});
这是允许我在上面的JS中打印值77的原因
令人烦恼的是,我想发送一个完全相同的对象列表。所以我使用以下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.parse(数据)
的人我用过这个:
var data;
var data2 = JSON.parse(data);
function testFunc() {
d3.select("#stuff").append("h2").text(data2[1].source);
}
修改
所以,当我逐步完成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);
它应该将上面的字符串放入&#39;数据&#39;然而,当我对它进行警报时,在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]
我对上述方法的字段变量进行了第二次测试,得到了
[object HTMLInputElement]
答案 0 :(得分: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);