2

I am getting either empty Json results or double json results and I don't know why yet?

base line: http://learn.knockoutjs.com/mail?folder=Inbox looks like this in chrome F12: {"id":"Inbox","mails":[{"id":1,......}

My Action:

public ActionResult Mail()
{
    string qs = "";
    foreach (var q in Request.QueryString)
    {

        qs += string.Format("{0}={1}&", q, Request.QueryString[q.ToString()]);
    }
    var proxyRequest = "http://learn.knockoutjs.com/mail?" + qs;
    var request = WebRequest.Create(proxyRequest);
    var response = (HttpWebResponse)request.GetResponse();



    var reader = new StreamReader(response.GetResponseStream());
    var str = reader.ReadToEnd();
    var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(str);

    //var json = JsonConvert.SerializeObject(data);  
        // Text Visualization looks good {"id":"Inbox","mails":[{"id":1,"from":"Abb....}
        // no outside quotes, no escaped quotes

        var res = Json(data, JsonRequestBehavior.AllowGet);
        return res;

}

So basically I am proxying the call, but the question is why is the result coming back as either not filled in or double jsoned.

using chrome network trace the result of the above looks like. [[[]],[[[[[]],[[]],[[]],[[]],[[]],...]

valid json, just empty.

when I change the code to look like this

var json = JsonConvert.SerializeObject(data);  
// Text Visualization looks good {"id":"Inbox","mails":[{"id":1,"from":"Abb....}
// no outside quotes, no escaped quotes

var res = Json(json, JsonRequestBehavior.AllowGet);
return res;

Chrome network trace has doubled jsoned the data. "{\"id\":\"Inbox\",\"mails\":[{\"id\":1,\"from\":\"Ab....}"

Since I don't know what the Json actually is, I did newton soft dynamic convert JsonConvert.DeserializeObject

That may be the problem behind my empty response?

Any help would really be appreciated. Thanks

1
  • If you're happy with the answer, can you click the check box next to the answer to accept it? Commented Oct 6, 2012 at 19:29

1 Answer 1

6

Your first method is failing because Json() doesn't understand the dynamic object returned from your JSON reader.
Your second method is failing because Json() is serializing a pre-serialized string.

You can return pre-serialized JSON data using Content(jsonString, "application/json")

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

3 Comments

I would hope the newtonsoft.json implementation would have an MVC extension to deal with dynamic objects as JsonResults. I know I will forget this in about 6 months. Thanks
@SLaks, I think this is a bad practice. Chrome has unpredictable effect to throw out in console.
@CodeFarmer: What are you talking about?

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.