我读了一个JSON文件,请参阅下面的当前格式。反序列化按预期工作。现在,客户向我提供了另一个格式略有不同的文件。该应用程序将来必须适应这两种格式。 在当前格式中,我基本上读取了值复制字段,我必须基本上以新格式执行相同操作。在新格式中,复制字段位于名为Assays的新节点内。我不确定如何读取子节点。
**Current Format**
"Orders": [
{
"Rack": "0015",
"SampleType": "Calibrator",
"Position": 1,
"CalMaterialLotNumber" : "08559LF00",
"CalMaterialExpirationDate" : "07-31-2012",
"LevelName" : "Cal 1",
"AssayNumber": 149,
"AssayVersion": 5,
"Dilution": 1,
"Replicate": 3,
"Track": 1,
"Lane": 1,
"ReagentMasterLot": "08559LF00",
"ReagentSerialNumber": 65000,
"
**New Format**
{
"MCCOrders": [
{
"Carrier": "Z425",
"Position": 1,
"CalMaterialLotNumber" : "03112I000",
"CalMaterialExpirationDate" : "02-28-2014",
"Assays": [
{
"AssayNumber": 1014,
"AssayVersion": 6,
"Dilution": 1,
"Replicate": 3,
"MasterLotNumber": "03112I000",
"PackSerialNumber": "20001",
"Comment": "TP Cal"
},
成员
public class CategoryTypeColl
{
public CategoryType[] MCCOrders { get; set; }
}
public class CategoryType
{
public int Replicate { get; set; }
}
处理JSON文件的方法
public static KeyValuePair<bool, int> CyclesCompleted(string fileName)
{
int cyclesCompleted = 0;
JavaScriptSerializer ser = jss();
bool isValid = true;
try
{
//Run Newtonsoft JSON deserializer
var deserialized = JsonConvert.DeserializeObject<CategoryTypeColl>(LoadTextFromFile(fileName));
CategoryTypeColl ctl = deserialized;
//CategoryTypeColl ctl = ser.Deserialize<CategoryTypeColl>(LoadTextFromFile(fileName));
if (ctl != null)
{
List<CategoryType> collection = (from item in ctl.MCCOrders
select item).ToList();
foreach (var replicates in collection)
{
cyclesCompleted = cyclesCompleted + replicates.Replicate;
}
}
}
catch
{
isValid = false;
}
return new KeyValuePair<bool, int>(isValid, cyclesCompleted);
}