1

I'm working with the mvc 4 web api to build a service layer that will always return JSON.

My api method calls actually call another service which returns a JSON object. I then want to just pass this JSON object back as my return object, but I'm not sure what return type to use.

If I use string, it wraps the JSON object in quotes.

By the way, I already changed the default MediaTypeFormatter to be JSON.

Here is an example of the JSON object:

{
  "responseHeader":{
  "status":0,
   "QTime":0,
   "params":{
     "indent":"on",
     "q":"id:100001",
     "wt":"json"}},
  "response":{"numFound":1,"start":0,"docs":[
  {
    "Header":"Test Header",
    "MaxPrice":515.0,
    "ApartmentName":"Apartment 1",
    "MaxBathrooms":4.0,
    "Pool":true,
    "MinBathrooms":2.0,
    "MaxBedrooms":4,
    "CoveredParking":false}]
  }}
0

3 Answers 3

7

In the Beta release, you can use JsonValue (from the System.Json namespace). If your call to the other service returns a string which contains the JSON data, then you can call JsonValue.Parse to load that into the object to return.

In the RC release (or in the current bits from http://aspnetwebstack.codeplex.com) you can use the JToken object (from the Newtonsoft.Json.Linq namespace) - the default JSON serializer and JSON DOM are now coming from the JSON.NET library.

5
  • I tried this, but it took my existing JSON object and created another JSON object with each property being a key-value pair. Commented May 9, 2012 at 19:18
  • What is the type of your "existing JSON object"? Can you post it? Commented May 9, 2012 at 21:06
  • I'm using a c# HttpWebRequest to call SOLR web service, so I'm converting the streamreader to a string. Commented May 10, 2012 at 12:48
  • Just to elaborate on this, the JsonValue was the correct way to take the HttpWebRequest stream and apply it to a Json object using JsonValue.Load(stream). The problem turned out to be that I was then serializing the JsonValue again before rendering the json output. Commented May 10, 2012 at 15:20
  • So, is it working now? Based on what I understand, here's what you need to do: HttpWebRequest -> GetResponse -> Stream -> JsonValue.Load(Stream) -> manipulate JsonValue (if necessary) -> return that JsonValue which you have. Commented May 10, 2012 at 17:56
0

So, the issue turned out to be that I was taking a serialized json object and then reserializing in the JavaScriptSerializerFormatter that overrides the default xml MediaTypeFormatter.

I fixed the issue by returning the JsonValue and then checking the type and not reserializing it.

Thanks carlosfigueira for the help finding this.

-2

To return Json from MVC, use JsonResult. You can first convert the string to an object using DataContractJsonSerializer.

http://shashankshetty.wordpress.com/2009/03/04/using-jsonresult-with-jquery-in-aspnet-mvc/

2
  • I'm hoping to do this without deserializing the JSON object before returning the value. Commented May 9, 2012 at 19:19
  • -1 This is pre MVC 4. Web Api (MVC 4) works a bit differently. Commented Oct 15, 2012 at 22:17

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.