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

Joh*_*han 11 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
}
]
Run Code Online (Sandbox Code Playgroud)

我得到错误:

Cannot deserialize JSON array into type '<>f__AnonymousType1`3[System.String,System.String,System.String]'.
Run Code Online (Sandbox Code Playgroud)

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

c#代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

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) {

            }
        });

    });
Run Code Online (Sandbox Code Playgroud)

zdr*_*rsh 17

  • 1.

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

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

            }
        });
Run Code Online (Sandbox Code Playgroud)

跳过这一步,如果在你的代码中,你得到inputFields字符串,{items: [{..}]}而不是像[{..}, {..}]我刚刚添加它用于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);
        }
Run Code Online (Sandbox Code Playgroud)