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}]
}}
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 callJsonValue.Parseto 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
JTokenobject (from the Newtonsoft.Json.Linq namespace) – the default JSON serializer and JSON DOM are now coming from the JSON.NET library.