3

On the server side I create a list

List<object> data = new List<object>();

for (var j = 0; j < items.Count(); j++) 
{ 
    var temp = groups.Where(customfilter); 
    data.Add(Html.Raw(Json.Encode(temp)));
}

System.Web.Script.Serialization.JavaScriptSerializer serializer = new 
    System.Web.Script.Serialization.JavaScriptSerializer(); 

var serializedData = serializer.Serialize(data);

Inside Javascript the folowing won't work for anything but primitive types.

var localData = @data;

This is the error:

System.Collections.Generic.List`1[System.Object]

What am I missing?

9
  • 5
    Serialize it into JSON and put into template. "What am I missing?" --- the fact, that C# has no idea what is javascript. Commented Aug 21, 2013 at 0:59
  • I tried that. It gives me something like this : [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]; Commented Aug 21, 2013 at 1:02
  • 1
    now what you're missing - is that C# doesn't know HOW to serialize Object. Commented Aug 21, 2013 at 1:03
  • System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var serializedData = serializer.Serialize(data); Won't this do it? Commented Aug 21, 2013 at 1:04
  • 1
    How would JavaScriptSerializer know what properties Object has? Why don't you use some more specific types? Commented Aug 21, 2013 at 1:04

2 Answers 2

1

I am assuming that the C# code you posted is a Controller action method.

You would need to have your controller method return a JsonResult, and then use a JSON library to deserialize it.

Something like

public class MyController: Controller
{
    public JsonResult MyAction ()
    {
        var data = new List<object>();
        for (var j = 0; j < items.Count(); j++) 
        { 
            var temp = groups.Where(customfilter); 
            data.Add(temp);
        }
        return Json (data, JsonRequestBehavior.AllowGet);
    }
}

Note how you don't need to serialize every item as you add it to the list.

From the client side, you can call your method and deserialize the result to a list. You can do it like this:

$.ajax ({ url:'/My/MyAction'}).done (function (data) {
    var myList = JSON.parse (data);
});

Edit:

After some testing, I found that you can access the data by doing:

var myList = data;

Not sure why, but "data" is sent directly as an array.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

0

First of all check this link http://james.newtonking.com/pages/json-net.aspx

Second

var temp = groups.Where(customfilter).ToList(); 

If you use where cast it to list or it will be something like Query object *(I dont remember exactly type) Or if you want to select one object try groups.FirstOrDefault

Third

 data.Add(temp);

Serializer will do his job to convert it.

4 Comments

I just tried it. I get the same result [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},‌​{},{}]; after serializing. Everything is fine until the point where the serialize() method is called.
Ok tell me You are selecting one item right? Then give me new part of code where you select data and add to list.
I am not sure I understand you. I tried the ToList() and there is no change. The problem is I am not able to serialize the JSON.If at all I don't do the Json transformation the serialization happens but again it is flawed and throws syntax errors.
I am not sure how native serializer works, did you tried json.net ?

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.