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"]);
7
  • How are you converting List to json? Commented Jun 11, 2013 at 15:16
  • @BhushanFirake - I have a script where I loop through each row and stringify it. I updated the post. Commented Jun 11, 2013 at 15:18
  • use JSON.stringify(data); in your code directly, no need of loop Commented Jun 11, 2013 at 15:22
  • Oh. I thought you were trying to generate a json-valid string in server side code, not javascript, oops. Commented Jun 11, 2013 at 15:22
  • @BhushanFirake - I did that and that put the [] around the string, but now I get the error message: Type 'JsonDictionaryObject' is not supported for deserialization of an array. in the line: ` public JsonDictionaryObject(string JsonString) : base(jss.Deserialize<JsonDictionaryObject>(JsonString)) { }` Commented Jun 11, 2013 at 15:41

1 Answer 1

1

Try this:

    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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.