views:

108

answers:

4

Hi,

I am trying to create the JSON string / object that is equivalent to the following data on the server side. can somebody help?

Public Shared Function GetData() As List(Of Employee)
    Dim list As New List(Of Employee)()

    Dim newEmployee As New Employee()

    newEmployee.EmployeeID = "1"
    newEmployee.FirstName = "Sridhar"
    newEmployee.Title = "Programmer"
    newEmployee.BirthDate = "8/10/1979"
    newEmployee.TitleOfCourtesy = "Programmer"
    list.Add(newEmployee)
    Return list
 End Function

Employee is a class with the properties EmployeeId, FirstName, Title, Birthdate, TitleOfCourtesy.

Thanks, sridhar.

A: 
rosscj2533
I tried this before but it didn't work. The list that the server side is returning is array of employees.
Sridhar
That should work. It's an object with the primary key "Employee" whose value is an object itself with various properties such as "EmployeeID" and "FirstName". That JSON is correct, so I don't know why it would return an array, unless it's your server side code...
Dustin
if I set the vairable as [{"EmployeeID":1,"LastName":"Duggireddy","FirstName":"Sridhar","Title":"Programmer","TitleOfCourtesy":"Programmer","BirthDate":new Date(303091200000)}]; it is working. But I am not sure how to create this programmatically.
Sridhar
> I believe multiple instances of Employee in the JSON would look like> this:>> <snip/>Actually, you're looking for an array. You can't use multiple objects successively as values like that. Here is the corrected code using arrays for multiple employees:{"Employee": [{"EmployeeID": "1", "FirstName": "Sridhar"}, {"EmployeeID": "2", "FirstName": "Joe"}]}Also, because arrays are indexed using integers, you could completely remove the EmployeeID field and just use integers for the EmployeeID field.
Dustin
@Dustin - thanks, I wasn't 100% sure on the syntax.@Sridhar - I've never created JSON client side like this, so I'd think you'd either have to manually build it up by creating a long string, or (probably an easier option) use something already made to do it like Sakabako suggests.
rosscj2533
A: 

Why not just use JSON.NET and let it handle encoding/decoding for you?

Josh Stodola
A: 

There's a good jQuery plugin for JSON. It lets you go from a JavaScript object to JSON very easily.

http://code.google.com/p/jquery-json/

sakabako
+1  A: 

Keep in mind that in Javascript there is no concept of a class, only objects. This also carries over into JSON. Look at this:

{"Employee" : 
     {
        "EmployeeID":"1",
        "FirstName":"Sridhar",
        etc...
     }
 }

If you look at the first line, the "Employee" symbol does absolutely nothing for the JSON. Remember that we're dealing with ONLY objects.

Thats why this works, like you said.

[
{"EmployeeID":1,
"LastName":"Duggireddy",
"FirstName":"Sridhar",
"Title":"Programmer",
"TitleOfCourtesy":"Programmer",
"BirthDate":new Date(303091200000)}
]

To make this programatically, declare your employee objects, and just add them into an array, like so:

var employees = [];
employees.push(employee1); // you would use a loop, of course
employees.push(employee2);
...
var jsonString = parser.toJSON(employees); // or whatever you use.

That should give you a list of objects. Always ignore the class in JSON... .NET during the deserialization will attempt to coerce the object into that particular class. You only have problems if this fails - maybe because a variable is missing or of the wrong type.

Sudhir Jonathan
thank you for the explanation. It worked perfectly. I didn't use parser.toJSON though. is it a standard javascript function or a plugin that I need to include?
Sridhar
No, what JSON parser you use depends on your library... you'll probably have to look at the docs for the correct method to use.
Sudhir Jonathan