10

Having a string containing the following raw Json data (simplified for the sake of the question):

  var MyString =  "{ 'val': 'apple' }";

How can I create a JsonResult object representing MyString?

I tried to use the Json(object) method. but it handles the raw json data as an string -logically :P-. So the returned HTTP response looks like:

"{ 'val': 'apple' }"

instead of the given raw Json Data:

{ 'val': 'apple' }

this is what I want to achieve:

SDReyes
  • 9,798
  • 16
  • 53
  • 92

3 Answers3

24

The Json() method on Controller is actually a helper method that creates a new JsonResult. If we look at the source code for this class*, we can see that it's not really doing that much -- just setting the content type to application/json, serializing your data object using a JavaScriptSerializer, and writing it the resulting string.. You can duplicate this behavior (minus the serialization, since you've already done that) by returning a ContentResult from your controller instead.

public ActionResult JsonData(int id) {
    var jsonStringFromSomewhere = "{ 'val': 'apple' }";
    // Content() creates a ContentResult just as Json() creates a JsonResult
    return Content(jsonStringFromSomewhere, "application/json");
}

* Starting in MVC2, JsonResult also throws an exception if the user is making an HTTP GET request (as opposed to say a POST). Allowing users to retrieve JSON using an HTTP GET has security implications which you should be aware of before you permit this in your own app.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Brant Bobby
  • 14,956
  • 14
  • 78
  • 115
  • 2
    +1 - but according to http://json.org though, the keys and values need to be enclosed in double quotes and not single. – Russ Cam Oct 21 '10 at 21:11
  • Nice catch. I just copied the string the asker used. Of course, this whole answer assumes that you *know* your JSON string is valid to begin with so the client's web browser doesn't choke on it. – Brant Bobby Oct 21 '10 at 21:18
1

The way I have generated json data from a string is by using JavaScriptResult in the controller:

public JavaScriptResult jsonList( string jsonString)
{
   jsonString = "var jsonobject = new Array(" + jsonString + ");";
   return JavaScript(jsonString)
}

Then when you request pass the json string to that action in your controller, the result will be a file with javascript headers.

quakkels
  • 11,676
  • 24
  • 92
  • 149
0

I think you can use the JavaScriptSerializer class for this

var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = js.Deserialize("{ 'val': 'apple' }", typeof(object));
Hector Correa
  • 26,290
  • 8
  • 57
  • 73
  • Would it work to serialize it as an object? I don't think the serializer adds properties to the serialized object, I think it just looks for properties on the object that match the properties in the JSON. Maybe you could try `dynamic` though? – Nick Larsen Oct 21 '10 at 21:00
  • This would deserialise the object only to have is serialised again. – BJury Jul 16 '15 at 09:05