1

I'm new to asp.net and visual basic.

I have an assigment where I must send queries to the database and the returns data. I would like to convert that data to a json and then use it with jQuery

My idea is make an array (arrayList?) and then convert it to json.

How can i use json with asp.net vb? I'm not allowed to use a webservice yet


I forgot to mention that this would be a new module in DNN

user3708642
  • 124
  • 10
chepe263
  • 2,774
  • 22
  • 38
  • Use any of the libraries discussed in [this question](http://stackoverflow.com/questions/571168/what-json-library-works-well-for-you-in-net/571200#571200). – Matthew Flaschen Mar 15 '12 at 22:15

2 Answers2

2

I wouldn't use an ArrayList, but have a look at the JavaScriptSerializer class.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

You can use the JsonSerializer class to do so

Here is an example creating a list of string objects and converting to json

Dim lstString As New List(Of [String])()
lstString.Add("One")
lstString.Add("Two")

Dim serializer As New JavaScriptSerializer()
Return serializer.Serialize(lstString)

Put this method in an ashx handler and you can access that from jQuery using the getJSON method.

http://api.jquery.com/jQuery.getJSON/

JavaScriptSerializerclass is available in the System.Web.Script.Serialization namespace.

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

You may probably convert it to an extension method like this and use it wherever you want

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks. It worked good. It returns an array, what if i want an object? Also, how can make it multidimensional? Like `var[0][3]` or `var.item.name;` `var.item.color;` ??? – chepe263 Mar 15 '12 at 22:52
  • what object ? you can send your object data as json and in the client you can access that.use the object inside the serialize method and you will get the json for that/ – Shyju Mar 15 '12 at 22:54