Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a json object in vb.net 2005

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.

like image 612
Abhishek Avatar asked Sep 08 '09 13:09

Abhishek


1 Answers

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

  1. Add a reference to Newtonsoft.Json, and an Import Newtonsoft.Json in your class.

  2. 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)
like image 117
aolde Avatar answered Oct 13 '22 00:10

aolde