Announcement

Collapse
No announcement yet.

How do I create on the fly json objects?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I create on the fly json objects?

    My function requires to pass a json object with some properties set, I wonder how do I do create one within C#?

    I tried to just creating a Class with the exact names of the fields I need, but that turns to generate some crazy complicated code:

    $t = Bridge.getEnumerator(Web.ExtensionClass.jQueryEnumerable($(".datepicker")));
    while ($t.moveNext()) {
        var obj = $t.getCurrent();
        var options = Bridge.merge(new Web.DatePickerOptions(), {
            defaultDate: obj.attr("value"),
            format: "YYYY.MM.DD"
        } );
        console.log(options);
    }
    I want to generate something like { defaultDate: obj.attr('value"), format: "YYYY.MM.DD" } instead of intantiating an entire new DatePickerOptions class

    #2
    You can mark your `DatePickerOptions` by `ObjectLiteralAttribute`
    or just use anonymous object like
    `new {defaultDate = DateTime.Now}`

    Comment


      #3
      Yeah, this works for me, thanks for the help.

      Comment


        #4
        Ok, I am encountering a problem, I have code like this:

                                input.css({
                                    'background-color': '#eb9316',
                                    'color'           : 'white'
                                });
        And I would like to do something like this:

        input.Css(new {
            background-color = " #eb9316",
            color = "white"
        });
        But I can't because it will interpret background - color as an expression and you can't have that on the left side. Is there any other way to create on the fly json objects which support names like 'background-color' ?

        Comment


          #5
          Try this
          var input = jQuery.Select("#myinput");
                      var opt = new
                      {
                          color = "white"
                      };
                      opt["background - color"] = "#eb9316";
          
                      input.Css(opt);

          Comment

          Working...
          X