使用NewtonSoft反序列化在c#中访问深度嵌套的JSON对象



我有一个非常困难的时间到达一些深嵌套的对象在我的JSON

我有一个目录,大约有500个JSON文件,我需要通读,并输出某些数据,所以我加载文件如下:

public static void getJsonFiles()
{
    int i = 0;
    string directory = @"Z:My_JSON_FILESDataFilesForAnalysisDataFilesAsJSON";
    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;
...

我已经使用json2sharp创建了所有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"
      }
    ]
  }

我做错了什么?我遵循了很多不同的例子,但它似乎都不起作用

写入

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

你使用object作为属性类型,这是错误的,这不是关于JSON反序列化

正如Volkan所说,问题不在于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; }
}

相关内容

  • 没有找到相关文章

最新更新