开发者

how do you convert a string into a Json object to return to a jquery getJson call

i have code in csharp that returns this string:

{'dateTimeFormat': 'iso8601', 'wikiURL': "http://simile.mit.edu/shelf/", 'wikiSection': "Simile Cubism Timeline", 'events' : [ {'start': '1924', 'title': 'Barfusserkirche', 'description': 'by Lyonel Feininger, Ameri开发者_运维问答can/German Painter, 1871-1956', 'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg', 'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm' } ] }

i want to ship this back to my view as a JSon structure, but it doesn't seem to be working:

here is my controller code:

   public JsonResult GetTimeLineJson(int id)
    {
        RoadmapItem item = new RoadmapItem();
        string timelineString = [There is a function here that returns the string above];

        return Json(timelineString);
    }

and here is my jquery code:

      var URL = "/Business/GetTimeLineJson/" + resourceId;

      $.getJSON(URL, function(data) {
             $('#deskView').show();
             onLoad(data);
       });

any idea whats going wrong here


In your example the string {'dateTimeFormat': ...-1924-Posters_i1116895_.htm' } ] } would be JSON-encoded and sent to the client, i.e. the data is double-encoded.

Would it be possible to skip the string timelineString =... line and pass the "raw" data to the Json() method?
Exactly what is [There is a function here that returns the string above] doing?

edit: Whatever you pass to Controller.Json(object) gets json/javascript-encoded (by Web.Script.Serialization.JavaScriptSerializer) before being sent to the client.
E.g. if you want to send an array with "a" and "b" as elements (json-encoded) the client must receive the string ["a","b"]. But if you pass ["a","b"] to Controller.Json() the client will receive "[\"a\",\"b\"]"

You probably want

return Content(timelineString, "application/json");

see Controller..::.Content Method


You can user this:

RoadmapItem item = new RoadmapItem();

return JsonConvert.SerializeObject(item);

JsonConvert is a member of Newtonsoft.Json.dll. You can reed about it and download it here: http://james.newtonking.com/projects/json-net.aspx

Of course method GetTimeLineJson can return string ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜