简体   繁体   English

C#JSON字符串到JavaScript数组

[英]C# JSON string to JavaScript array

I have the following class in C#: 我在C#中有以下课程:

public class Caption
{
    public string line;
    public string timestamp;
}

I have created some Caption objects and added them to a List: 我创建了一些Caption对象并将其添加到列表中:

List<Caption> CaptionList = new List<Caption>();
var obj = new Caption
{
  line = "Some text"
  timestamp = "00:10"
};
CaptionList.Add(obj);

I then use Json.NET to serialize the list to Json, like this: 然后,我使用Json.NET将列表序列化为Json,如下所示:

public string json = JsonConvert.SerializeObject(CaptionList);

Now I want to use this as a JavaScript array, so I tried this: 现在,我想将此作为JavaScript数组使用,所以我尝试了以下方法:

var arr = '<%=json%>';

I don't have much JavaScript knowledge, but it seems like arr is now a string and not an array because json is in fact a C# string. 我没有太多的JavaScript知识,但是好像arr现在是一个字符串而不是一个数组,因为json实际上是C#字符串。 I tried accessing the objects by using arr[i].line etc, but that doesn't work. 我尝试使用arr[i].line等访问对象,但这不起作用。

How do I make the json an actually array in JavaScript? 如何将json变成JavaScript中的实际数组?

You can use the JSON.parse method to parse the JSON string into an array: 您可以使用JSON.parse方法将JSON字符串解析为一个数组:

var arr = JSON.parse('<%=json%>');

However, as JSON is a subset of JavaScript syntax, you can actually use the JSON as a JavaScript array literal: 但是,由于JSON是JavaScript语法的子集,因此您实际上可以将JSON用作JavaScript数组文字:

var arr = <%=json%>;

Take it as demo 当作演示

Your variable arr will be rendered like this. 您的变量arr将以这种方式呈现。 This will be string. 这将是字符串。 so to convert a string into JSON Object you need to parse that string. 因此要将字符串转换为JSON对象,您需要解析该字符串。 by using JSON.parse(arr) 通过使用JSON.parse(arr)

var arr = '[ { "line":"Some text", "timestamp":"00:10" }, { "line":"Some text", "timestamp":"00:10" } ]';

var JObject= JSON.parse(arr);

Now you can access JObject[0].line

This will give you 这会给你

"Some text" “一些文字”

You can take a look Here at Mozilla 您可以在这里查看Mozilla

We use MVC and in our views (which pass up models) we use a simple line whenever we want to convert the model into a javascript object: 我们使用MVC,并且在我们想要将模型转换为javascript对象的视图中(通过模型),我们使用一条简单的行:

<script type="text/javascript">
        $(document).ready(function () {
            MyObject.Initialize(@Html.Raw(Json.Encode(Model)));
        });
</script>

The key to all of that being: 所有这些的关键是:

@Html.Raw(Json.Encode(Model))

"Model" is what MVC passes up but you could very easily do something like this: (Razor syntax) MVC传递了“模型”,但是您可以很容易地执行以下操作:(Razor语法)

@{
   List<Caption> CaptionList = new List<Caption>();
   var obj = new Caption
   {
     line = "Some text"
     timestamp = "00:10"
   };
   CaptionList.Add(obj);
}

then... 然后...

<script type="text/javascript">
        $(document).ready(function () {
            MyObject.Initialize(@Html.Raw(Json.Encode(CaptionList)));
        });
</script>

I haven't tested this but it should work, or some variation of it, you may need to put "CaptionList" as a property in some parent object and pass that into Json.Encode. 我尚未对此进行测试,但是它应该可以工作,或者它可以进行一些变型,您可能需要将“ CaptionList”作为属性放入某个父对象中,并将其传递给Json.Encode。

You could do this instead, as well: 您也可以这样做:

<script type="text/javascript">
        $(document).ready(function () {
            var myArray = @Html.Raw(Json.Encode(CaptionList)));
        });
</script>

Note that jQuery isn't necessary here, it was just a way to wrap it up in the ready function. 请注意,这里不需要jQuery,这只是将其包装在ready函数中的一种方法。

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

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