2

I am executing a linq query which gets me all the records from a table:

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

The above returns the the value:

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

but according to jsonlint, this is invalid and I need to have it be returned as:

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

How can I do this?

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

Deserialize using `JavaScriptSerializer:

var people = jss.Deserialize<List<People>>(args["xldata"]);
4

1 回答 1

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);
于 2013-06-11T16:04:15.843 回答