使用JSON.NET将JSON反序列化为匿名对象

时间:2012-01-13 14:56:35

标签: c# asp.net json json.net

我正在使用JSON.NET对一个对象进行deserlaize,但是我无法使用它来处理我正在使用的对象的当前结构。

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

我的对象目前看起来很喜欢这个(我想传递一个对象列表)

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

我得到错误:

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

我需要的是与示例#2类似的东西,一个包含列表的容器对象。任何帮助表示赞赏。感谢

c#c​​ode:

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 个答案:

答案 0 :(得分:17)

  • 1

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

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

            }
        });

跳过此步骤,如果在您的代码中,您获得了像{items: [{..}]}这样的inputFields字符串,而不是像[{..}, {..}]那样我只是为了mu测试目的而添加它。重要的是以这种格式[{..}, {..}]

获取字符串inputFields
  • 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);
        }