【问题标题】:Serialize C# dynamic object to JSON object to be consumed by javascript将 C# 动态对象序列化为 JSON 对象以供 javascript 使用
【发布时间】:2010-03-31 13:24:45
【问题描述】:

基于示例c# dynamic with XML,我修改了DynamicXml.cs 并解析了我的xml 字符串。 修改部分如下

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;
        if (binder.Name == "Controls")
            result = new DynamicXml(_elements.Elements());
        else if (binder.Name == "Count")
            result = _elements.Count;
        else
        {
            var attr = _elements[0].Attribute(
                XName.Get(binder.Name));
            if (attr != null)
                result = attr.Value;
            else
            {
                var items = _elements.Descendants(
                    XName.Get(binder.Name));
                if (items == null || items.Count() == 0)
                    return false;
                result = new DynamicXml(items);
            }
        }
        return true;
    }

要解析的xml字符串:

               "< View runat='server' Name='Doc111'>" +
                    "< Caption Name='Document.ConvertToPdf' Value='Allow Conversion to PDF'></ Caption>" +
                    "< Field For='Document.ConvertToPdf' ReadOnly='False' DisplayAs='checkbox' EditAs='checkbox'></ Field>" +
                    "< Field For='Document.Abstract' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< Field For='Document.FileName' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< Field For='Document.KeyWords' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< FormButtons SaveCaption='Save' CancelCaption='Cancel'></ FormButtons>" +
                "</ View>";

动态表单 = new DynamicXml(markup_fieldsOnly);

有没有办法将此动态对象的内容(动态内的名称值对)form 序列化为 JSON 对象并发送到客户端(浏览器)?

【问题讨论】:

    标签: c# json dynamic


    【解决方案1】:

    我听说Json.Net 效果很好,虽然我自己从未使用过。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 2011-05-30
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      相关资源
      最近更新 更多