如何将此JSON格式返回到字符串数组中

时间:2013-06-11 15:13:39

标签: c# json linq javascriptserializer

我正在执行一个linq查询,它从表中获取所有记录:

var data = _context.People.ToList(); //_context is my DataContext.

以上返回值:

{ "name": "john", "age": "30" }, { "name": "jane", "age": "31" }

但是根据jsonlint,这是无效的,我需要将其返回为:

[{ "name": "john", "age": "30" }, { "name": "jane", "age": "31" }]

我该怎么做?

 viewData.xldata = [];
         $.each(data, function(i, row) {
            var strRow = JSON.stringify(row);
            viewData.xldata.push(strRow);});

使用`JavaScriptSerializer:

反序列化
var people = jss.Deserialize<List<People>>(args["xldata"]);

1 个答案:

答案 0 :(得分:1)

试试这个:

    List<People> data= _context.People.ToList();

   System.Web.Script.Serialization.JavaScriptSerializer objSerializer = default(System.Web.Script.Serialization.JavaScriptSerializer);
   objSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

    return objSerializer.Serialize(data);