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

时间:2022-08-23 09:41:30

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.

我正在使用JSON.NET对一个对象进行deserlaize,但我不能让它与我正在使用的对象的当前结构一起工作。

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

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

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

c# code:

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:

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 个解决方案

#1


16  

  • 1.
  • 1。

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

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 [{..}, {..}]

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

  • 2.
  • 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);
        }

#1


16  

  • 1.
  • 1。

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

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 [{..}, {..}]

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

  • 2.
  • 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);
        }