因此,我使用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]
请添加样品JSON –
'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
@PHPglue我可以看到,但我没有写。它就在我的浏览器调试器中。这就是发送到浏览器的内容。 – nhoughto