MashukKhan
MashukKhan

Reputation: 1954

How to get data from server in JSON format by using ASP.NET c#?

How to get data from server in JSON format by using ASP.NET c#?

I know how to get data in JSON format using php but i'm trying to do the same but using c#.

Upvotes: 1

Views: 1447

Answers (1)

Tomislav
Tomislav

Reputation: 3211

Consider following when you post your JSON from server side:

  1. data: JSON.stringify(person),

  2. contentType: "application/json"

On your client side:

  1. get the request body from HttpCurrent.Context.Request.InputStream.

  2. read the input stream and convert to string

  3. deserialize the json object

Do it like this::

string json;
using(var reader = new StreamReader(Request.InputStream)){
        json = reader.ReadToEnd();
    }
var person = Json.Decode(json);

Tutorial: http://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages

How to: Request Data Using the WebRequest Class

Upvotes: 3

Related Questions