Question

If I have the following post call:

 $('#json_form').submit(function (event) {

            event.preventDefault();
            var url = $(this).attr('action');
            var datos = {
                "uno": "lalala",
                "dos": "jojojo"
            }
            var data = JSON.stringify(datos);

            $.post(url, data, function (resultado) {
                $('#posted_values').html(resultado);
            });
        });

How can I receive and process the json object in a cshtml file? I mean what I put in Decode call:

if (IsPost)
    {
        var json_object = Json.Decode(Request???);
}

Edited to complete the answer of @MikeBrind, to help others with the same problem. Example of using decode for a more complex json object.

     $('#json_form').submit(function (event) {

            event.preventDefault();
            var url = $(this).attr('action');
            var datos = {
                "firstName": "John",
                "lastName": "Smith",
                "age": 25,
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "New York",
                    "state": "NY",
                    "postalCode": 10021
                },
                "phoneNumber": [
                    {
                        "type": "home",
                        "number": "212 555-1234"
                    },
                    {
                        "type": "fax",
                        "number": "646 555-4567"
                    }
                ]
            }

            var data = JSON.stringify(datos);

            $.post(url, {"person": data}, function (resultado) {
                $('#posted_values').html(resultado);
            });
        });

Receiving and using:

@{
    dynamic json_object;    
    if (IsPost)
    {
        json_object = Json.Decode(Request["person"]);
        @json_object.firstName<br/>
        @json_object.lastName<br/>        
        @json_object.address.city<br/>
        @json_object.address.postalCode<br/>
        foreach (dynamic phone in json_object.phoneNumber)
        {
            @phone.type<br/>
            @phone.number
        }

    }    
}
Was it helpful?

Solution

Don't JSON.stringify the data if it is just key/value pairs like this. Do a form post:

$('#json_form').submit(function (event) {
    event.preventDefault();
    var url = $(this).attr('action');
    var datos = {
        "uno": "lalala",
        "dos": "jojojo"
    }
    //var data = JSON.stringify(datos); no need for this

    $.post(url, datos, function (resultado) {
        $('#posted_values').html(resultado);
    });
});

Then the values are available from Request["uno"] and Request["dos"]

If you ever do need to use JSON.stringify (which you would for more complex data structures), the JSON is transmitted in the Request body, so you need to extract it from Request.InputStream:

var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top