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

时间:2016-11-14 17:53:56

标签: c# json json.net

我很难在JSON中找到一些深层嵌套的对象

我有一个目录,其中包含大约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)访问深层嵌套对象时,我收到错误&#39; Object不包含FinalReport的定义没有扩展方法FinalReport ...&#39;

我的对象定义如下:

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"
      }
    ]
  }

我做错了什么?我已经关注了很多不同的例子,但它似乎无法正常工作

2 个答案:

答案 0 :(得分:1)

写出像

这样的属性
public ResultsReport ResultsReport { get; set;  }

public FinalReport FinalReport { get; set; }

您使用对象作为属性类型,这是错误的,它不是关于JSON反序列化。

答案 1 :(得分:0)

正如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; }
}