简体   繁体   English

读取JSON序列化列表<string>

[英]Read a JSON Serialized List<string>

I have an ordinary List<string> that I convert to JSON with System.Web.Script.Serialization; 我有一个普通的List<string> ,可以通过System.Web.Script.Serialization;转换为JSON System.Web.Script.Serialization;

The result is something similar to this 结果类似于此

[
  "string1",
  "string2",
  "string3",
  "string4",
  "string5",
  "string6"
]

Now how do I read this in jQuery? 现在我如何在jQuery中阅读? I prefer to be able to loop them out one by one. 我希望能够将它们一一列出。 Or am I supposed to make a better JSON object and return that, if so are there any good ways to make a JSON object from a List<string> ? 还是我应该制作一个更好的JSON对象并返回它,如果可以的话,有什么好的方法可以从List<string>制作JSON对象吗?

Use an AJAX request in order to get the List to the client and the assign it to a JavaScript Array object: 使用AJAX请求以将List获取到客户端,并将其分配给JavaScript Array对象:

var list = new Array();
$.ajax({
     url: myUrl,
     success: function(data){
         list = data;
     }
});

You can next iterate the list in order to access each element, either with jQuery ( $.each ), or with regular JavaScript: 接下来,您可以使用jQuery( $.each )或常规JavaScript来访问list ,以访问每个元素:

for (var i = 0; i < list.length; i++){
   //do whatever with *list[i]*
}

You can read it by looping it using $.each 您可以使用$.each循环读取它

$.each(yourList, function (index, value) {
    console.log(index, value);
});

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

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