我想访问List<Records>
其中 Records 是来自 JS 脚本的结构。经过一番谷歌搜索(特别是 question 29098558 这里),我 Json 序列化了我的对象并在脚本中检索它。
我应该提到这是我第一次接触 JS,所以语法可能完全错误。
在 C# 中
string serializedResults = JsonConvert.SerializeObject(RecordsList);
(在我的上下文中,记录实际上位于 Dictionnary<string, Records>
中,但我通过字典隐藏了访问权限,因为我认为它与此处无关)
在 JavaScript 中
<script type="text/javascript">
function setupBarChart() {
var x = [];
var y = [];
var source = "<%= serializedRecords %>";
// I would like to put Records fields in y and x tab to use it as data for the axes of an histogram.
for (var i = 0; i < source.length; i++) {
}
}
</script>
我的问题是:
- 来源的类型是什么?它只是一个字符串吗?它是否创建了 JS 中 List 的等价物?
- 如何从 JS 访问我的记录字段?
- source.length 是否给出了列表中的记录数?
最佳答案
what is the type of source ? Is it just a string ? Does it create an equivalent of List in JS ?
在显示的代码中,它是一个字符串,但我认为显示的代码是非常错误的 - 引号不正确,应该删除,以:
var source = <%= serializedRecords %>;
(注意,我不会对上面的编码发表意见,但是:要小心并检查它是否安全)
离开(在渲染的 html 中):
var source = [{"foo":123,"bar":"def"},{...},{...}];
现在类型是一个 JavaScript 数组。
How do I access my Records fields from JS ?
通过 source
,这是一个数组
Does source.length gives me the number of records in the list ??
是的,如果你丢失了引号
关于javascript - 从 Javascript 后面的代码获取 C# 结构列表的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43253160/