访问使用NewtonSoft反序列化
问题描述:
我有一个非常困难的时期达到了我的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"
}
]
}
我在做什么错?我跟着这么多不同的例子,但它只是不会似乎工作
答
写的属性,如
public ResultsReport ResultsReport { get; set; }
public FinalReport FinalReport { get; set; }
您正在使用对象属性类型,那是错误的,它是不是JSON反序列化。
答
正如沃尔坎所言,问题不在于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; }
}