简体   繁体   English

将JSON字符串/数组解析为JS对象

[英]Parse JSON string/array into JS objects

So I have used JSON to serialize a list of objects from C# to send to JS. 因此,我已经使用JSON对来自C#的对象列表进行序列化以发送给JS。 The list seems to arrive at the browser but I can't figure out how to use it correctly. 该列表似乎到达了浏览器,但我不知道如何正确使用它。 It makes sense that what arrives is a string however it seems to actually be an array... I'm not sure and I can't figure out how to use it. 到达的是字符串是有意义的,但实际上似乎是一个数组...我不确定,我不知道如何使用它。

Here is my JS 这是我的JS

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

When I send a single object the above JS prints out the value properly. 当我发送单个对象时,上述JS会正确打印出该值。 Here is that C# 这是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);
        }

When I look at the browser's debugger I see this line below when the above is executed: 当我查看浏览器的调试器时,执行上述操作时会在下面看到此行:

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

This is what allows me to print the value 77 in the JS above 这就是我可以在上面的JS中打印值77的原因

The annoying thing is that I want to send a list of this exact same object. 令人讨厌的是,我想发送此完全相同的对象的列表。 So I use the following C# 所以我用下面的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);
        }

When I use the same JS it won't print anything out however the list is getting to the JS because I see below when I look at the browser's debugger: 当我使用相同的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}]);

So, since the list of data is in the browser how do I use it? 那么,既然数据列表在浏览器中,该如何使用呢?

PS Not really necessary but here is my testClass if anyone is curious 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()
        {

        }
    }

EDIT 编辑

For those suggesting I have tried using JSON.parse(data) 对于那些建议我已经尝试使用JSON.parse(data)

I used this: 我用这个:

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

EDIT 编辑

So when I step through the C# the line: 因此,当我逐步执行C#时,代码行如下:

JsonConvert.SerializeObject(L);

puts the following string into the json var: 将以下字符串放入json变量:

"[{\"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}]"

Then in theory when I call: 然后理论上我打电话给:

ClientScript.RegisterArrayDeclaration("data", json);

it should put the above string into the 'data' var in the js however when I do an alert on it as such: 它应该将上面的字符串放入js中的“ data”变量中,但是当我对它进行警告时:

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

What appears is 出现的是

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

I also tried another method in my C#: 我也在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);
        }

Which required the following change to the JS 需要对JS进行以下更改

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

When I try this new method I get the same as before: 当我尝试这种新方法时,会得到与以前相同的结果:

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

And I did a second test on the field var from the method above and got 然后根据上述方法对字段var进行了第二次测试,得到了

[object HTMLInputElement]

just use JSON.parse() 只需使用JSON.parse()

for example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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