0

Im learning JSon. All i want to do i is to to send back some data and make it into a .NET object.

Ive been trying different tutorials i think i kind of understand it. Below is my code. What i dont understand is, what in the request object is it that holds the Json data and how do i deserialize it?

My Javascript

 $(function () {
        // Do your stuff here

        alert("wööööö");

    urlToHandler = 'JSonTestHandler.ashx';
    jsonData = '{ "FavoriteFood":"Pasta with butter and cheese", "FavoriteSport": "Submission Wrestling", "FavoriteGame": "Starcraft 2", "FavoriteMusic": "Hip Hop" }';
    $.ajax({
        url: urlToHandler,
        data: jsonData,
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json',
        success: function (data) {
            setAutocompleteData(data.responseDateTime);
        },
        error: function (data, status, jqXHR) {
            alert('There was an error.');
        }
    }); // end $.ajax


});

My JSon Handler

[WebService(Namespace = "http://localhost:53243")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JSonTestHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        var json = new JavaScriptSerializer();

        var memoryStream = new MemoryStream();
        var serializer = new DataContractJsonSerializer(typeof(PersonalPreferences));

        memoryStream.Position = 0;

        var personalPreferenceFromJson = (PersonalPreferences)serializer.ReadObject(memoryStream);

    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

My Object that holds the Json data

        [DataContract]
public class PersonalPreferences
{
    [DataMember]
    public string FavoriteFood { get; set; }

    [DataMember]
    public string FavoriteSport { get; set; }

    [DataMember]
    public string FavoriteGame { get; set; }

    [DataMember]
    public string FavoriteMusic { get; set; }

    public PersonalPreferences()
    {
    }
}
7
  • 1
    Take a look at the json.net library as well (json.codeplex.com) Commented Sep 20, 2012 at 15:02
  • Ive heard about it, but im trying to learn how to do it with the normal .NET library, unless someone with experience tells me its not supposed to be done like that. Commented Sep 20, 2012 at 15:08
  • Ah ok, the JSON.net library is quite widely used and is supposed to be faster and more efficient. Commented Sep 20, 2012 at 15:12
  • Thanks! Ill probably look it up, but i want to learn it without extra libraries first. :) Commented Sep 20, 2012 at 15:12
  • This might also help: williamsportwebdeveloper.com/cgi/wp/?p=494 Commented Sep 20, 2012 at 15:25

1 Answer 1

0

Actually i think that the only thing i need is to figure out where in the Request object i can find the actual string that i should parse to an objekt! I cant find anything.

I found it, to get it from the Request object i use

string jsonData = new StreamReader(context.Request.InputStream, System.Text.Encoding.UTF8).ReadToEnd();
Sign up to request clarification or add additional context in comments.

Comments

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.