john chesley

fresh stuff

destinations

favorites

json and vb/asp .net

i've been doing some web development lately, using asp.net and visual basic (disclaimer: not by choice, but to be compatible with all the other web apps in the little organization i'm working for). i've also tossed in a little jquery here and there and everywhere, and have become a big fan of ajax.

i came to a point in the development process when things were starting to come together, but complexity of my app was starting to escalate, and i needed a more flexible way to send messages from the server to the browser. of course i had heard of something called JSON ...about all i knew is that django uses it for database dumps, and it seems to be a pretty flexible little format for doing whatever the hack you want and or need. it's been described as the low-fat alternative to XML, but by "low-fat" we're not talking "all the flavor taken out" as much as "all the ridiculous crap you don't really need, or want-- taken right out".

so i start looking around for how to use json in .net...asp..vb..anything. there's a few things out there, but what they call "serializers" in the .net framework work with objects. that's great, when you're into serializing your entire object, but i just needed something lightweight that i could use to send only certain messages/data. a few google searches didn't turn up much, so i decided to just write my own simple little json class. it's not complicated, and doesn't fully support all datatypes, but it works for what i need it to work for: sending the data i need, in a format that javascript can interpret and use easily.

' Json.vb: an extremely simplified implementation of JSON [www.json.org]
'
' July 17, 2009
' John Chesley
'
' a Json object lets you easily and dynamically build JSON
' data strings that can be passed back and interpreted by
' a javascript program
'
' use Json.add(key, value) to add a key/value pair
' and use Json.json to access the entire JSON string
'
' ex:
' Dim j as new Json
' j.add("date", "9/1/2009")
' Response.write(j.json)
'
' will send the string {"date":"9/1/2009"} to the client. if it was requested by
' javascript, this data can very easily be interpreted and used as a javascript Object
' see http://docs.jquery.com/Ajax/jQuery.getJSON for an example of
' requesting and using JSON data
'

Public Class Json
    Private _json As String
    Private _length As Integer

    Public Sub New()
        _json = ""
        _length = 0
    End Sub

    Public ReadOnly Property length() As Integer
        Get
            Return _length
        End Get
    End Property

    Public ReadOnly Property json()
        Get
            Return String.Format("{{{0}}}", _json)
        End Get
    End Property

    Public Sub add(ByVal key As String, ByVal value As String)
        If Me.length <> 0 Then
            _json += ","
        End If
        _json += String.Format("""{0}"":""{1}""", escapeQuotes(key), escapeQuotes(value))
        _length += 1
   End Sub

   Public Shared Function escapeQuotes(ByVal s As String) As String
        Return s.Replace("""", "\""")
   End Function

End Class

it even escapes quotes all by itself, so you don't have to clutter up the code you call this from. don't you just love how many quotes you have to put in to get out just a single double quote in visual basic? it's ridiculous.

but there you have it. customizable json strings in visual basic! you can download it, too.