0

I'm looking for a way to render a C# class object to javascript. For instance this class definition:

public class Foo
{ 
   public string Bar1 { get; set; }
   public string Bar2 { get; set; }
}

should render (an object of class Foo with values filled) as:

foo: 
{
    bar1: 'bar1value',
    bar2: 'bar2value'
}

I know my way around reflection, but before reinventing the wheel all over again I was wondering if there are any libraries already doing these kind of things.

4 Answers 4

2

This might do what you want: http://json.codeplex.com/

There's an example as well: http://james.newtonking.com/projects/json/help/

If I recall, JSON demands quotes for key names (though probably tolerates a lack thereof), but I wasn't sure about Javascript, so I tested it with the following HTML, which seems to be usable in Chrome, IE, and Firefox.

<html>
<body>
    <script type="text/javascript">
        o = { "a":1, "b":2 };
        alert(o.a +" " +o.b) ; 
    </script>
</body>
</html>

Hopefully, you'll be able to use such syntax directly (RFC 4627 says that JSON is a subset of Javascript).

Sign up to request clarification or add additional context in comments.

5 Comments

That's exactly what I needed, thank you very much. Perhaps you have an answer to my follow up question too? stackoverflow.com/questions/8028069/…
Welcome. I'm not sure my answer to your other question will help too much, though.
Hmmm actually I'm not quite sure if this is indeed what I need. The difference: json.net outputs foo { "bar1": "bar1value" } where I need: foo { bar1 : "bar1value" } (note the missing quotes). If I need to do lot's of custom things to get it working, I'm better off writing my own serialization handlers.
I've added a sort of a test case. The quote syntax also works with Rhino.
Yeah, it seems it doesn't really matter if there are quotes or not. So I'm now using the code with quotes and that seems to work ok. Thanks
2

You can serialize your object to JSON.

See my answer here : Any way to pass an object from c# code behind to javascript?

Comments

0

You're probably trying to pass a C# object to the view through post.

[HttpPost]
public JsonResult GetDataController(
    string param1,
    string param2) 
{
    var myFoo = new Foo
    {
        Bar1 = "Param1 is: " + param1,
        Bar2 = "Param2 is: " + param2
    };

    return Json(myFoo);
}

And now, on your view, request the object through ajax:

$.ajax({
    type: 'POST',
    datetype: 'json',
    url: '@Url.Action("GetDataController")',
    data: {
        param1: "line1",
        param2: "line2"
    },
    success: function (data, textStatus, xhr) {
        alert("I've received this JSOn object:\n\n" + data);
    },
    error: function (xhr, textStatus, errorThrow) {
        alert("Error while requesting JSON");
    }

3 Comments

this answer is too specific to MVC.
Ups, you're right! I though I had arrived here through one of favorited tags asp.net-mvc-3.
Nope, not creating controller logic!
-1
 Dim RegisteredUsers As New List(Of Person)()
        RegisteredUsers.Add(New Person With {.PersonID = 1, .Name = "Bryon Hetrick", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 2, .Name = "Nicole Wilcox", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 3, .Name = "Adrian Martinson", .Registered = False})
        RegisteredUsers.Add(New Person With {.PersonID = 4, .Name = "Nora Osborn", .Registered = False})
    Dim serializer As New JavaScriptSerializer()
    Dim serializedResult = serializer.Serialize(RegisteredUsers)
    ' Produces string value of: 
    ' [ 
    '     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true}, 
    '     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true}, 
    '     {"PersonID":3,"Name":"Adrian Martinson","Registered":false}, 
    '     {"PersonID":4,"Name":"Nora Osborn","Registered":false} 
    ' ] 

    Dim deserializedResult = serializer.Deserialize(Of List(Of Person))(serializedResult)
    ' Produces List with 4 Person objects 

2 Comments

The question is tagged with C#.
Sorry , dont know C#, i thought to provide with a guidance as much as i could.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.