Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
If you're happy with the answer, can you click the check box next to the answer to accept it? –  McGarnagle Oct 6 '12 at 19:29
add comment

1 Answer

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")

share|improve this answer
1  
Success: var res = Content(str, "application/json"); worked.. –  Herb Stahl Sep 12 '12 at 15:40
    
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 –  Herb Stahl Sep 12 '12 at 15:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.