1

I am using a method of passing Json to and from my markup using jquery and ajax. This can be described in more detail on this page: http://blogs.telerik.com/aspnet-ajax/posts/12-04-27/the-present-and-future-of-using-json-in-webforms.aspx

In this snippet of code, I try to set the object's value dynamically by setting a string variable named "test" to a business object's value:

       Dim objOrder As Object = New JsonObject()
       For Each Order As BoVendorOrder In Orders
            Dim Vendor As New BoVendor(Order.VendorID)
            Dim test As String = Order.VendorOrderID
            objOrder.test = Vendor.VendorName + " - " + Order.VendorOrderPoNumber
       Next

I left out some code for the sake of brevity. The goal is to get the objOrder.test to be equal to the VendorOrderID (a number in our SQL database) so that the JSON looks like this:

       {
            "123456": "VendorName - PONumber",
            "678901": "VendorName - PONumber"
       }

Any of you guys out there know how to do this?

Primalpat
  • 374
  • 4
  • 17

1 Answers1

1

Do you really need the order IDs to be properties of the object? It might be easier to just return a serialized Dictionary(Of String, String). You could still look up by order ID and it would be easier to loop over than the props of the Javascript object.

Here's an example of what you'd need to do using the dictionary approach:

Dim OrdersDict as New Dictionary(Of String, String)()  
For Each Order as BoVendorOrder In Orders  
    If Not OrdersDict.ContainsKey(Order.VendorOrderID) Then  
        OrdersDict.Add(Order.VendorOrderID, Vendor.VendorName + " - " + Order.VendorOrderPoNumber)
    End If
Next

' Serialize the dictionary object to JSON

' Using System.Web.Script.Serialization.JavascriptSerializer:
Dim Serializer As New JavaScriptSerializer
If MaxLength Then Serializer.MaxJsonLength = Int32.MaxValue
Dim x as String = Serializer.Serialize(OrdersDict) 'Return or response.write x as needed

'or

'Using JSON.net
Dim x as String = JsonConvert.SerializeObject(OrdersDict) 'Return or response.write x as needed
Brent Keller
  • 1,385
  • 1
  • 12
  • 17
  • No, I don't need the orderIDs as properties of the object. I have a workaround in place that is functioning currently. I'm basically building a string and adding the orderIDs as properties that way. The code is a bit tedious / ugly, and I wanted to know if this was a possibility. As for your suggestion, I have never used a Dictionary(Of String, String) before. I am preparing the json to be looped through in my markup using jquery, so I wont need to lookup orderIDs with vb. Ill google Dictionary(Of String, String) and see what that looks like. Thanks =) – Primalpat Feb 25 '14 at 14:19
  • Hey, really appreciate the example! I follow it pretty well until the MaxLength part. It is not defined for me while following your example in my project. I've imported `System.Web.Script.Serialization` already. Also, this is MUCH cleaner than what I was doing as a workaround. – Primalpat Feb 25 '14 at 20:03
  • Sorry I copied this out of some code I had. We pass in a boolean MaxLength that if set to True sets the max length of the resulting json string so we don't write too much. I think we added that because we ran into issues with really really long arrays full of objects with lots of properties. You can probably ignore that in this case. – Brent Keller Feb 25 '14 at 20:33
  • This works great, and provides much cleaner code than manually building a string. I would still like to know if creating a property of a JSonObject is possible, but I am marking this as the answer because it solves my problem. Thanks Brent! – Primalpat Feb 26 '14 at 13:22