HenrikP
HenrikP

Reputation: 884

Read a JSON Serialized List<string>

I have an ordinary List<string> that I convert to JSON with 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? 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>?

Upvotes: 0

Views: 365

Answers (2)

Andrei V
Andrei V

Reputation: 7506

Use an AJAX request in order to get the List to the client and the assign it to a JavaScript Array object:

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:

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

Upvotes: 2

Praveen
Praveen

Reputation: 56509

You can read it by looping it using $.each

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

Upvotes: 1

Related Questions