2012-01-13 17 views
11

Im usando JSON.NET deserializzare un oggetto, ma non riesco a farlo funzionare con la struttura corrente dell'oggetto che sto usando.Deserializzare JSON su oggetto anonimo utilizzando JSON.NET

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

mio oggetto attualmente sembra liks questa (voglio passare un elenco di oggetti)

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

Im ottenendo l'errore:

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

cosa ho bisogno è qualcosa di simile per esempio # 2, un oggetto contenitore contenente un elenco. Qualsiasi aiuto è apprezzato. Grazie

codice 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

È possibile aggiungere altro codice per ottenere un contatto? Il codice chiamante e quindi l'utilizzo del risultato – Mharlin

+0

@Mharlin aggiornato – Johan

risposta

16
  • 1.

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

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

      } 
     }); 

saltare questo passaggio se nel codice, si ottiene la stringa inputFields come {items: [{..}]} e non come [{..}, {..}] l'ho appena aggiunto per scopo di test mu. La cosa importante è ottenere un InputFields stringa in questo formato [{..}, {..}]

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

Sembra una soluzione, ma non posso provarlo ora. Non conoscevo la parte 'new []'. Grazie dell'aiuto! – Johan

+0

funziona per me. spero che sia d'aiuto. – zdrsh

Problemi correlati