I want to create a json object in vb.net to send the response back javascript function to do something please tell me if anyone have any idea about it.
If you can't get access to DataContractSerializer (which is in .NET 3.5) then you can use Json.NET from Newtonsoft. I've used it in the past and found it to be excellent.
As you're using .NET 2.0 you have to use the JSON libary by James, with download at Codeplex (version for .NET 2.0).
An example of using Json.NET
Add a reference to Newtonsoft.Json, and an
Import Newtonsoft.Json
in your class.How to serialize an object (Product is only an example object, change this to your own object):
Dim product As New Product() product.Name = "Apple" product.Expiry = New DateTime(2008, 12, 28) product.Price = 3.99D product.Sizes = New String() {"Small", "Medium", "Large"} 'Call SeralizeObject to convert the object to JSON string' Dim output As String = JavaScriptConvert.SerializeObject(product)
The output
variable will hold the value:
{
"Name": "Apple",
"Expiry": "\/Date(1230375600000+1300)\/",
"Price": 3.99,
"Sizes": [
"Small",
"Medium",
"Large"
]
}
Another example would be to convert an array of strings.
Dim myArray As String() = {"Hello", "World"}
Dim jsonString As String = JavaScriptConvert.SerializeObject(myArray)
Hi, I'm newbie in json.
Still not understand how to create an JSON object in vb.net. "Dim product As New Product()", error type product not define.
Ok I get it : (for other newbies to json)
Public Class Product
Public name As String
Public expiry As date
Public price As integer
Public sizes as array
End Class