views:

152

answers:

3

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.

+1  A: 

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.

http://www.codeplex.com/Json

free-dom
ya its okey i find it but how to i use it please me by example, please
Abhisheks.net
+3  A: 

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)
Manticore
thank dear but i am unable to implement. please tell me about product, what is it, i tried it by making a class but there is some problem so please tell me a little bit more.
Abhisheks.net
I'm sorry. Product object was only meant as an example, and is not needed. I will edit my answer to clarify this. If you give us the object you need to JSON convert then we can help you implement it!
Manticore
okey and thank you so much to reply me again. here i am describe you what i want to in my json object.there is 5 columns for each entry like this..say First enrty A ["Name", "Age", "Gender", "Location]. Second Enrty B ["Name", "Age", "Gender", "Location].this will go on as the entries will be there.please tell me .
Abhisheks.net
okey now i will do it thanks a lot.
Abhisheks.net
hello buddy, now i have a another problem with json, in which format the json object should be returned to javascript function .
Abhisheks.net
Just try to `Response.Clear()` and `Response.Write(jsonString)` in the codebehind.
Manticore
no i was ask that, but its OK, it did it. Thank a lot to help me.
Abhisheks.net
You're welcome!
Manticore
A: 

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