2016-11-14 103 views
0

我有一個非常困難的時期達到了我的JSON一些深度嵌套的對象在C#中嵌套很深的JSON對象訪問使用NewtonSoft反序列化

我有大約500 JSON文件,我需要通讀,並輸出目錄從某些數據,所以我載入的文件是這樣的:

public static void getJsonFiles() 
{ 
    int i = 0; 
    string directory = @"Z:\My_JSON_FILES\DataFilesForAnalysis\DataFilesAsJSON"; 
    string[] jsonPath = Directory.GetFiles(directory, "*.json"); 
    foreach(string item in jsonPath) 
    { 
     jsonReader(item, i); 
     i++; 
    } 
} 

一旦我有文件加載,我通過File.ReadAllText閱讀它,所以我這樣做:

public static void jsonReader(string item, int i) 
{ 
    string readJson = File.ReadAllText(item); 
    RootObject rootObj = JsonConvert.DeserializeObject<RootObject>(readJson); 

    var resReport = rootObj.ResultsReport; 
... 

我使用json2csharp創建了所有JSON的對象,但是當我嘗試使用點符號(rootObj.ResultsReport.FinalReport.VariantProperties.VariantProperty.VariantName)訪問深度嵌套的對象時,出現錯誤'對象不包含FinalReport的定義並且沒有擴展方法FinalReport ...'

我對象的定義是這樣的:

public class VariantProperty 
{ 
    public string geneName { get; set; } 
    public string isVUS { get; set; } 
    public string variantName { get; set; } 
} 

public class VariantProperties 
{ 
    public string[] VariantProperty { get; set; } 
} 

public class FinalReport 
{ 
    public Object Application { get; set; } 
    public string ReportId { get; set; } 
    public string SampleName { get; set; } 
    public string Version { get; set; } 
    public Object Sample { get; set; } 
    public string PertinentNegatives { get; set; } 
    public Object Summaries { get; set; } 
    public Object VariantProperties { get; set; } 
    public Object Genes { get; set; } 
    public Object Trials { get; set; } 
    public Object References { get; set; } 
    public Object Signatures { get; set; } 
    public Object AAC { get; set; } 
} 

public class ResultsReport 
{ 
    public Object FinalReport { get; set; } 
    public Object VariantReport { get; set; } 
} 

public class RootObject 
{ 
    public Object ResultsReport { get; set; } 
} 

的JSON看起來是這樣的:

"ResultsReport": { 
"CustomerInformation": null, 
"FinalReport": { 
    "@xmlns:xsd": "http://www.w3.org/2001/XMLSchema", 
    "@StagingId": "XXXXXXXX", 
    "@clinicalId": "XXXXXXXX", 
    "Application": { 
    "ApplicationSettings": { 
     "ApplicationSetting": { 
     "Name": "Statement", 
     "Value": "XXXXXXXX" 
     } 
    } 
    }, 
    "ReportId": "XXXXXXXX", 
    "SampleName": "XXXXXXXX", 
    "Version": "1", 
    "Sample": { 
    "FM_Id": "XXXXXXXX", 
    "SampleId": "XXXXXXXX", 
    "BlockId": "XXXXXXXX", 
    "TRFNumber": "XXXXXXXX", 
    "TestType": "XXXXXXXX", 
    "SpecFormat": "XXXXXXXX", 
    "ReceivedDate": "XXXXXXXX" 
    }, 
    "PertinentNegatives": null, 
    "Summaries": { 
    "@alterationCount": "XXXXXXXX", 
    "@clinicalTrialCount": "XXXXXXXX", 
    "@resistiveCount": "XXXXXXXX", 
    "@sensitizingCount": "XXXXXXXX" 
    }, 
    "VariantProperties": { 
    "VariantProperty": [ 
     { 
     "@geneName": "BARD1", 
     "@isVUS": "true", 
     "@variantName": "P358_S364del" 
     }, 
     { 
     "@geneName": "GATA2", 
     "@isVUS": "true", 
     "@variantName": "P161A" 
     }, 
     { 
     "@geneName": "LRP1B", 
     "@isVUS": "true", 
     "@variantName": "V4109I" 
     }, 
     { 
     "@geneName": "MLL2", 
     "@isVUS": "true", 
     "@variantName": "P1191L" 
     }, 
     { 
     "@geneName": "NTRK1", 
     "@isVUS": "true", 
     "@variantName": "G18E" 
     }, 
     { 
     "@geneName": "NUP98", 
     "@isVUS": "true", 
     "@variantName": "A447T" 
     }, 
     { 
     "@geneName": "TET2", 
     "@isVUS": "true", 
     "@variantName": "D1121Y" 
     }, 
     { 
     "@geneName": "WT1", 
     "@isVUS": "true", 
     "@variantName": "T377_G397>S" 
     } 
    ] 
    } 

我在做什麼錯?我跟着這麼多不同的例子,但它只是不會似乎工作

回答

1

寫的屬性,如

public ResultsReport ResultsReport { get; set; } 

public FinalReport FinalReport { get; set; } 

您正在使用對象屬性類型,那是錯誤的,它是不是JSON反序列化。

0

正如沃爾坎所言,問題不在於JSON反序列化。我通過構建我的類來獲得json的工作,如下所示:

public class VariantProperty 
{ 
    public string geneName { get; set; } 
    public string isVUS { get; set; } 
    public string variantName { get; set; } 
} 

public class VariantProperties 
{ 
    public List<VariantProperty> VariantProperty { get; set; } 
} 

public class FinalReport 
{ 
    public Object Application { get; set; } 
    public string ReportId { get; set; } 
    public string SampleName { get; set; } 
    public string Version { get; set; } 
    public Object Sample { get; set; } 
    public string PertinentNegatives { get; set; } 
    public Object Summaries { get; set; } 
    public VariantProperties VariantProperties { get; set; } 
    public Object Genes { get; set; } 
    public Object Trials { get; set; } 
    public Object References { get; set; } 
    public Object Signatures { get; set; } 
    public Object AAC { get; set; } 
} 

public class ResultsReport 
{ 
    public FinalReport FinalReport { get; set; } 
} 

public class RootObject 
{ 
    public ResultsReport ResultsReport { get; set; } 
} 
相關問題