shawleigh17
shawleigh17

Reputation: 1187

Read JSON data in a C# page?

I post data to a aspx page via ajax, and now i don't know how to read it. I just need to take the strings that are passed via JSON and assign them to certain variables and manipulate the variables in the .aspx file. How do I do this?

Here is the jquery:

var ceSaveDatea = {};
    ceSaveDatea.one = requestNumber;
    ceSaveDatea.two = itemTypeID;
    ceSaveDatea.three = servicesRequired;
    ceSaveDatea.four = otherComments;
    ceSaveDatea.five = suggestedReplacements;
    ceSaveDatea.six = internalTestingRequired;
    ceSaveDatea.seven = externalTestingRequired;
    ceSaveDatea.eight = ceGeneralComments;

    /*
    var url = "../ajaxURLs/ComponentEngineering.aspx?requestNumber=" + requestNumber + "&itemTypeID=" + itemTypeID + "&servicesRequired=" + servicesRequired + "&otherComments=" + otherComments + "&suggestedReplacements=" + suggestedReplacements + "&internalTestingRequired=" + internalTestingRequired + "&externalTestingRequired=" + externalTestingRequired + "&ceGeneralComments=" + ceGeneralComments;
    var encodedURL = EncodeURL(url);
    */

    $.ajax({
        type: "POST",
        url: "/ajaxURLs/ComponentEngineering.aspx",
        data: JSON.stringify(ceSaveDatea),  
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function (data) {
            alert(data);
            if (data != '')
                $('#checkboxContainerDiv').html(data);
        },
        error: function(msg) {
        alert('error');
        }
    });

Then I just need to take this information and convert it on the ComponentEngineer.aspx page. It is not a web service.

Thanks!

Upvotes: 0

Views: 584

Answers (2)

dylansweb
dylansweb

Reputation: 684

The following code should do what you need:

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Dictionary<object, object> jsonLookup = ((Dictionary<object, object>)serializer.Deserialize<Dictionary<object, object>>(jsonSource));

The 'jsonSource' passed into the Deserialize method is your JSON string.

Prerequisites are:

  • Include a reference to the .NET assembly: System.Web.Extensions
  • Include a using statement at top of your class file: using System.Web.Script.Serialization;

Upvotes: 0

Rich Turner
Rich Turner

Reputation: 10984

If you want to handle JSON in your C# code, I strongly encourage you to go explore JSON.NET

Upvotes: 1

Related Questions