0

I have developed WebAPI for mobile which should return JSON and it is working fine returning JSON but JSON comes under XML.

Below is my code and screenshot.

return JsonConvert.SerializeObject(result);

"result" is my response class in which I am filling data. I tried with JavaScriptSerializer and JsonConvert of Newsoft but failed.

Json returned in XML

How can I get it work with simple string in JSON?

k-s
  • 2,192
  • 11
  • 39
  • 73
  • Try setting `Response.ContentType = "application/json";` [MSDN Reference](https://msdn.microsoft.com/en-us/library/ms525208%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396) and [application/json](http://www.iana.org/assignments/media-types/application/json) and [RFC 7159](https://tools.ietf.org/html/rfc7159) – Kraang Prime Dec 24 '16 at 08:54
  • Thank you but where I need to do that? – k-s Dec 24 '16 at 08:56
  • You need to do it before any data is sent from the server on the server. What seems pretty standard is to add code to accept a new get flag to determine the format. If flag is not set, then render as whatever you choose as default. eg `https://example.com/api/foo?type=json` – Kraang Prime Dec 24 '16 at 08:57
  • 2
    Do not return a JSON string. Just return the the result instance and the client can decide to get it as JSON or XML – Sir Rufo Dec 24 '16 at 08:58
  • @Samuel - I tried it and json gets converted but shows in XML as shown in above image. – k-s Dec 24 '16 at 09:00
  • @SirRufo Client is actually from mobile devices so they expect JSON as string. – k-s Dec 24 '16 at 09:00
  • The your client should set a header in the request that it will accept json. That is the whole story. WepApi can return both JSON or XML and let the client decide which one – Sir Rufo Dec 24 '16 at 09:02
  • This link might help if you are developing the web api from the ground up [Force web api to return plain text](http://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text) (just change plain text to `application/json`). If you are using a pre-built framework, then you will need to check it's docs / modify it's code base / or scrap it. – Kraang Prime Dec 24 '16 at 09:02
  • Show the entire API method. Is it `public string Get()`? – CodeCaster Dec 24 '16 at 09:04
  • This link here, may help in the event you ALWAYS want json > [Always return json IIS](http://stackoverflow.com/questions/12629144/how-to-force-asp-net-web-api-to-always-return-json) – Kraang Prime Dec 24 '16 at 09:04
  • BTW Internet Explorer will only accept XML by default. It is a very bad practice to test a WebApi with a browser. Use f.i. Postman (Chrome Application) to really test the WebApi – Sir Rufo Dec 24 '16 at 09:06
  • @Samuel, Thank you, it worked. Also this url helped..http://stackoverflow.com/questions/19975811/how-to-force-asp-net-web-api-to-return-json-or-xml-data-based-on-my-input – k-s Dec 24 '16 at 09:39
  • Guys, keep do down voting even if question is right and rightly described. – k-s Dec 24 '16 at 09:41
  • I never downvoted your question – Kraang Prime Dec 24 '16 at 09:47
  • Your question is not "right and righly described", relevant code is missing as I pointed out in my previous comment. If you followed that link from @Samuel and applied it, your API will be returning JSON, but that JSON will be a JSON string (`" { \"IsError\":0, \"Message\":\"City List\", ... "`), which is definitely not what you're looking for. – CodeCaster Dec 24 '16 at 11:06
  • @CodeCaster, If question do not describe problem and people are not able to understand then you can say relevant code is missing - It is already there. Whatever part of code is required to make people understand it is there.. if you do not understand and review detailed code then its your problem. – k-s Dec 24 '16 at 11:09
  • The relevant part of this problem is the return type of the method, which you don't show. You're the one having a trivial question, and I've dealt with Web API more than enough to know what code is relevant. Also, as @SirRufo said, you don't need to do `JsonConvert.SerializeObject()` yourself, which is also part of the problem at hand. – CodeCaster Dec 24 '16 at 11:09
  • @CodeCaster - I apologize and will take care next time. Thank you for making me aware. I really appreciate your time and comment on my question. – k-s Dec 24 '16 at 11:11
  • I'm sure you don't, but I really don't care about that. Again, if you apply the solution from that link or from the answer below, you are going to run into a new problem, being that your client cannot parse the JSON. Anyway, good luck. – CodeCaster Dec 24 '16 at 11:12
  • So you mean the solution in this link wont work? It is returning as a string in JSON. http://stackoverflow.com/questions/19975811/how-to-force-asp-net-web-api-to-return-json-or-xml-data-based-on-my-input – k-s Dec 24 '16 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131413/discussion-between-k-s-and-codecaster). – k-s Dec 24 '16 at 13:20
  • i had modified my answer it is not worked yet? – Rajput Dec 24 '16 at 19:55

1 Answers1

1

Since you have converted your only method return type to json and not the whole data(response data) to Json format. For returning whole response data to Json, add following syntax to your WebApiConfig.cs Class.

config.Formatters.Remove(config.Formatters.XmlFormatter);

Since Web Api sends all response data to XML by default. So you need to remove default XMLFormatter from WebApiConfig.cs class.

For better understanding of MediaTypeFormatters please have a look on this video tutorial.

https://www.youtube.com/watch?v=tNzgXjqqIjI&list=PL6n9fhu94yhW7yoUOGNOfHurUE6bpOO2b&index=6

UPDATE

Dont serialize object in method. remove JsonConvert.SerializeObject() and return your object. Simply add above syntax in WebAppconfig.cs file.

Rajput
  • 2,597
  • 16
  • 29
  • This is not the answer. The OP is returning a **string** from their method. If the OP adds this, their API wil respond with JSON... but the JSON itself **will be a string**. So instead of `{ "foo" : "bar"}`, the response will be `"{ \"foo\" : \"bar\" }"`. – CodeCaster Dec 24 '16 at 11:04
  • he need to remove this JsonConvert.SerializeObject from return type. – Rajput Dec 24 '16 at 11:18
  • Hi Rajput, Thank you for the answer - I have applied the same thing as per my comment but applied json formatting using HttpwebRequest. – k-s Dec 27 '16 at 13:14