2012-01-13 90 views
11

使用JSON.NET進行確定性對象,但我不能讓它與當前使用的對象結構一起工作。將JSON反序列化爲使用JSON.NET的匿名對象

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

      } 
     }); 

    }); 
+0

你可以添加一些代碼來獲得contxt?調用代碼,然後結果的使用 – Mharlin 2012-01-13 15:04:01

+0

@Mharlin更新 – Johan 2012-01-13 15:06:03

回答

16
  • 1.

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

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

      } 
     }); 

跳過這一步,如果在你的代碼中,你會得到inputFields字符串,如{items: [{..}]},而不是像[{..}, {..}]我只是爲了mu測試目的而添加它。最重要的是這種格式[{..}, {..}]

  • 2.

得到一個字符串inputFields。

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); 
     } 
+0

看起來像一個解決方案,但現在不能測試它。不知道這個'new []'部分。感謝您的幫助! – Johan 2012-01-13 16:18:20

+0

適合我。希望能幫助到你。 – zdrsh 2012-01-13 16:19:58

相關問題