0

我读了一个 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);
    }
4

0 回答 0