0

I am using a controller to create a JSON string.

StringBuilder builder = new StringBuilder();
...
builder.Append(String.Format(@"{{""lat"":{0}, ""lon"":{1}}},", latitude, longitude));

When it comes to the view, the code above is rendered into something like

"lat":10.7654200827348, "lon":106.681716282384}

so jQuery.parseJSON() does not work because it does not understand " How can I fix that? Thank you in advance

1
  • 1
    How are you rendering this in the view? Could you post that code? Commented Mar 31, 2013 at 14:08

2 Answers 2

2

Never create JSON manually like this. You could have a controller action returning a JsonResult:

public ActionResult SomeAction()
{
    var model = new
    {
        lat = latitude,
        lon = longitude
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

And then you could consume this action using an AJAX call:

<script type="text/javascript">
    $.getJSON('@Url.Action("SomeAction")', function(result) {
        // you could use result.lat and result.lon here directly
        alert('latitude: ' + result.lat + ', longitude: ' + result.lon);
    });
</script>

In this case you don't need to be building or parsing any JSON manually. This plumbing will be handled by the framework for you.

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

4 Comments

Hi Darin, I followed your idea but the rendered page just contains my json. The source of the page does not contain any HTML tag, just Json in it. How can I fix it? Moreover, what is the pros and cons of your idea in comparison with ViewBag? What is the better practice?
Which page is containing only JSON? Where are you calling the $.getJSON function? Did you do that in the click handler of some submit button or an anchor? If so make sure you have canceled the default action by returning false from this handler or the browser will simply navigate away leaving no time for the AJAX request to execute. As far as ViewBag is concerned I don't see how and where you would use that? If you wanted to serve the JSON directly into the view without doing an AJAX call you could also do that by using the view model and serializing it into JSON.
Please bear with me here. Here is my code $(document).ready(function () { $.getJSON('@Url.Action("Index")', function (result) { alert('latitude: ' + result.Latitude + ', longitude: ' + result.Longitude); }); }); About ViewBag, I plan to put my JSON in it and access that JSON in my view. How does it sound to you?
You are using result.Latitude and result.Longitude instead of result.lat and result.lon as shown in my answer. Actually that will depend on how you named the properties of the anonymous object you returned from the controller action. You may use a javascript debugging tool such as FireBug to inspect the AJAX request for potential errors. Concerning the ViewBag, that sounds like a very bad idea.
0

I don't know if this fixes your problem... but anyways you should follow this advice!

Use the JavaScriptSerializer from the System.Web.Extensions.dll to generate your JSON!

var serializer = new JavaScriptSerializer();
builder.Append(serializer.Serialize(new { lat = latitude, lon = longitude }));

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.