1

I build a ASP.NET Web Api. Now, i have update the project to MVC 4 RC, but the dynamic object is not working as expected anymore.

I have this:

var Arr = new JsonArray();

foreach (var Post in Coll)
{
    dynamic Item = new JsonObject();
    Item.Header = Post.Header;
    Item.Body = Post.Body;
    Item.Language = Post.Language;
    Arr.Add(Item);
}

return Request.CreateResponse(HttpStatusCode.OK, Arr);

The result i get from this is (In the browser):

[{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]}]

In the debugg i get this: (The object Arr)

{[{"Header":"All distances","Body":"All distances are ","Language":"EN"},{"Header":...

Why is it different?

(Coll is a List of objects)

3 Answers 3

0

Try to use it

return Request.CreateResponse(HttpStatusCode.OK, Arr.ToArray());

It is only a guess, if you can see it in the debugging then probably you force the calculating the object when you add to the watch list and the watcher displays it. And then .ToArray() should also help you.

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

Comments

0

I guess u didn't parse Arr to json data at client side. try this in client side ajax method:

 var obj = $.parseJSON(result);

result is the value return from server. and u can get each object of array as below:

        $(obj).each(function () {       
      // alert($(this).attr('Header'));  
 });

Comments

0

I had the same problem, I have fixed it inserting the Content in this way:

var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(MY_OBJECT, Formatting.Indented), Encoding.UTF8, "application/json");
return response;

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.