11

Im using JSON.NET do deserlaize an object, but i cant get it to work with the current structure of the object that im using.

http://dorobantu.me/post/2010/08/22/Deserializing-JSON-to-anonymous-types-in-C.aspx

My object currently looks liks this (i want to pass a list of objects)

[
{
    "ID": "Concurrent User",
    "FieldType": 190,
    "value": ""
},
{
    "ID": "System Type",
    "FieldType": 191,
    "value": null
}
]

Im getting the error:

Cannot deserialize JSON array into type '<>f__AnonymousType1`3[System.String,System.String,System.String]'.

What i need is something similar to example #2, a container object containing a list. Any help is appreciated. Thanks

c# code:

public void GetPoints()
    {
        string inputFields = HttpContext.Current.Request["inputFields"];

       // var test =  new { ID = string.Empty, FieldType = string.Empty, Description = string.Empty };

        var example = new { containerArray = new { ID = string.Empty, FieldType = string.Empty, Description = string.Empty } };

        var fields = JsonConvert.DeserializeAnonymousType(inputFields, example);
    }

javascript:

$('.quoteonly :input').live('change keyup', function () {

        var $container = $('#quoteonly-container');
        var containerObject = {};

        var containerArray = [];

        $container.find('.quoteonly :input').each(function () {

            var fieldType = $(this).data('fieldtype');
            var id = $(this).data('id');

            var currentObject = { 'ID': id, 'FieldType': fieldType };

            switch (fieldType) {

                case 190: //textbox
                    currentObject.value = $(this).val();
                    break;
                case 191: //select
                    currentObject.value = $(this).val();
                    break;
                case 192: //radio
                    currentObject.value = $(this).prop('checked') == true ? 1 : 0;
                    break;
                case 193: //checkbox
                    currentObject.value = $(this).prop('checked') == true ? 1 : 0;
                    break;
            }

            containerArray.push(currentObject);
            containerObject.containerArray = containerArray;
        });

        $.ajax({
            url: '../SentinelOperationsUI/GenericHandler.ashx',
            data: { 'FunctionName': 'GetPoints', 'inputFields': JSON.stringify(containerObject) },
            success: function (data) {

            }
        });

    });
1
  • Can you add some more code to get a contxt? The calling code and then the usage of the result Commented Jan 13, 2012 at 15:04

1 Answer 1

17
  • 1.

var DTO = { 'items': JSON.stringify(containerObject) };

$.ajax({
            url: '../SentinelOperationsUI/GenericHandler.ashx',
            data: JSON.stringify(DTO),
            success: function (data) {

            }
        });

skip this step if in your code, you get the inputFields string like {items: [{..}]} and not like [{..}, {..}] I just added it for mu testing purpose. The important thing is to get a string inputFields in this format [{..}, {..}]

  • 2.

.

 public void GetPoints()
        {
            string inputFields = HttpContext.Current.Request["items"];
            var test = new[] { new { ID = 0, FieldType = string.Empty, Description = string.Empty } };
            var fields = JsonConvert.DeserializeAnonymousType(inputFields, test);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like a solution, cant test it now though. Didnt know about the new[] part. Thanks for helping!

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.