leen
leen

Reputation: 9

how to build json object using .net and out it in http request

I'm building a mobile application that recive data from a server as json object , im using asp.net / sql server database to build the server , i dont know how to retrive data from sql database and convert it to json object that the client can request I'm not using asp.net MVC any help to start !

Upvotes: 0

Views: 245

Answers (4)

J. Polfer
J. Polfer

Reputation: 12481

You need to build some sort of web service that handles this request.

Here's a couple of options.

  • You could use Web API to write the service and use its automatic serialization / deserialization helpers
  • You could also write up a class that implements IHttpHandler, parse the incoming request using a JSON deserializer library, and serialize a JSON response using the same serializer library. This might be simpler than using Web API, especially if you need to deal with query string parameters in a way that is odd when using the automatic parameter functionality in Web API.
  • There is also a way to do this using WCF, although it's even more complicated than the above.

Upvotes: 0

Markus
Markus

Reputation: 22456

Check out JSON.net. It is a JSON serialization framework that has also been adopted by ASP.NET MVC Web API. You can install it via NuGet.

Upvotes: 1

TGH
TGH

Reputation: 39248

If you're using ASP.NET MVC you can create a controller that returns JSON. In your controller do:

return Json(yourObject);

You can also do it using the Web API. It will do content negotiation for you and return the data in the format you request it (JSON, XML, etc.).

Web API reference: Getting Started with ASP.NET Web API 2

Upvotes: 0

nikolai.serdiuk
nikolai.serdiuk

Reputation: 762

You can use JavaScriptSerializer class, for example:

SomeType obj = ....

JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(obj);

Then you should instruct browser to download json, here you can see: Returning JSON object from an ASP.NET page

Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();

You can use other serializers too: http://habrahabr.ru/post/133778/

Upvotes: 0

Related Questions