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


我正在使用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#代码:

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

            }
        });

    });

6371
2018-01-13 14:56


起源

你能添加一些代码来获得一个contxt吗?调用代码然后使用结果 - Mharlin
@Mharlin更新了 - Johan


答案:


  • 1。

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

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

            }
        });

如果在您的代码中,您将获得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);
        }

16
2018-01-13 16:05



看起来像一个解决方案,但现在无法测试它。不知道的 new[] 部分。谢谢你的帮助! - Johan
适合我。希望能帮助到你。 - zdrsh