C# 序列化包含数组的对象

C# 序列化包含数组的对象,c#,javascript,asp.net-mvc-3,knockout.js,knockout-mapping-plugin,C#,Javascript,Asp.net Mvc 3,Knockout.js,Knockout Mapping Plugin,-这个问题我自己解决了- 所以我试着使用knockoutJS并测试映射插件,我在序列化包含和数组的对象时遇到了一些问题 下面是我的控制器和下面的javascript,我的问题是无论我尝试什么(你可以看到注释掉的修复尝试),我都无法让子数组传递给javascript以显示子对象-{“id”:5,“name”:“Testing this works”,“children”:“[{},{}]”}就是传递的所有内容 有人能给我指一下正确的方向吗 namespace TestingKnockout.Con

-这个问题我自己解决了-

所以我试着使用knockoutJS并测试映射插件,我在序列化包含和数组的对象时遇到了一些问题

下面是我的控制器和下面的javascript,我的问题是无论我尝试什么(你可以看到注释掉的修复尝试),我都无法让子数组传递给javascript以显示子对象-
{“id”:5,“name”:“Testing this works”,“children”:“[{},{}]”}
就是传递的所有内容

有人能给我指一下正确的方向吗

namespace TestingKnockout.Controllers
{ 
public class child
{
    int id;
    String name;

    public child(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }   

    public virtual string GetData()
    {
        List<child> childrenList = new List<child>(){
                new child(2, "bob"),
                new child(4, "dave")
        };
        var result = new
        {
            id=5,
            name="Testing this works",
            children = childrenList
            //children = Newtonsoft.Json.JsonConvert.SerializeObject(childrenList)
            //children = new{ id = 2, name = "bob" }

        };

        //String resultsToHighlightJSON =                    Newtonsoft.Json.JsonConvert.SerializeObject(childrenList);
        //return resultsToHighlightJSON;
        return new JavaScriptSerializer().Serialize(result);// +resultsToHighlightJSON;
    }
}
}
namespace TestingKnockout.Controllers
{ 
公营儿童
{
int-id;
字符串名;
公共子项(int-id,字符串名)
{
this.id=id;
this.name=名称;
}
}
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}   
公共虚拟字符串GetData()
{
List childrenList=新列表(){
新生儿(2名,“鲍勃”),
新生儿(4名,“戴夫”)
};
var结果=新
{
id=5,
name=“测试此功能”,
儿童=儿童名单
//children=Newtonsoft.Json.JsonConvert.SerializeObject(childrenList)
//children=new{id=2,name=“bob”}
};
//字符串resultsToHighlightJSON=Newtonsoft.Json.JsonConvert.SerializeObject(childrenList);
//返回resultsToHighlightJSON;
返回新的JavaScriptSerializer();
}
}
}
这是我的javascript:

<script language="javascript" type="text/javascript">
  var originalData = {
      id: 1,
      name: "Main",
      children: []
  };

  var updatedData = {
      id: 1,
      name: "Main",
      children: [{ id: 2, name: "bob" }, { id: 3, name: "ted"}]
  };

  function Child(id, name) {
      this.id = ko.observable(id);
      this.name = ko.observable(name);
  }

  var options = {
      children: {
          key: function (data) {
              return ko.utils.unwrapObservable(data.id);
          }
      }
  }

  var viewModel = ko.mapping.fromJS(originalData, options);

  viewModel.counter = 1;
  viewModel.addChild = function () {
      viewModel.children.push(new Child(++viewModel.counter, "new"));
  };



  viewModel.applyUpdate = function () {
  var basePath="<%: Url.Content("~/") %>";
  var url = basePath + 'Home/GetData/';
   $.get(url, function (response) {
              var Employee = $.parseJSON(response);
              $("#EditLink").html("testing this out : " + Employee.children[1].name);
      //ko.mapping.fromJS( updatedData,viewModel);
      ko.mapping.fromJS( Employee,viewModel);
              });
  }

  $(document).ready(function() {
      ko.applyBindings(viewModel);
  });

原始数据变量={
id:1,
名称:“主要”,
儿童:[]
};
var updatedata={
id:1,
名称:“主要”,
孩子:[{id:2,名字:“bob”},{id:3,名字:“ted”}]
};
函数子项(id、名称){
this.id=ko.可观察(id);
this.name=ko.observable(name);
}
变量选项={
儿童:{
键:功能(数据){
返回ko.utils.unwrapobbservable(data.id);
}
}
}
var viewModel=ko.mapping.fromJS(原始数据,选项);
viewModel.counter=1;
viewModel.addChild=函数(){
viewModel.children.push(新的子对象(++viewModel.counter,“new”);
};
viewModel.applyUpdate=函数(){
var basePath=“”;
var url=basePath+'Home/GetData/';
$.get(url、函数(响应){
var Employee=$.parseJSON(响应);
$(“#EditLink”).html(“测试:“+Employee.children[1].name”);
//fromJS(updatedata,viewModel);
fromJS(雇员,视图模型);
});
}
$(文档).ready(函数(){
应用绑定(视图模型);
});

GetData()函数中尝试此操作

    return Newtonsoft.Json.JsonConvert.SerializeObject(result);
我从来没有遇到过JSON.NET的任何问题,而且我知道它支持匿名类型

public class child
{
    public int id { get; set; }
    public String name { get; set; }

    public child(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

我的错误是我没有设置公共属性

您是否使用Fiddler查看JSON实际返回给客户端?传递的只是{“id”:5,“name”:“Testing this works”,“children”:“[{},{}]”}当我使用应用更新函数时,这不是因为您无法在C#中序列化通用列表吗?尝试将其更改为数组,看看是否有效。@PaulManzotti不,我更改为数组,但下面仍然传递空数组,这是我对不起作用的数组所做的更改child child 1=new child(2,“bob”);child child2=新孩子(4,“dave”);child[]childrenList=新子[]{child1,child2};我以前已经以各种形式尝试过了(正如您可能从我的注释行中注意到的那样)。由于一些未知的原因,数组仍然作为空数组传递