使用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) {
}
});
});
你可以添加一些代碼來獲得contxt?調用代碼,然後結果的使用 – Mharlin 2012-01-13 15:04:01
@Mharlin更新 – Johan 2012-01-13 15:06:03