[英]Accessing deeply nested JSON objects in c# using NewtonSoft Deserialization
I am having a very difficult time reaching some deeply nested objects in my JSON 我很难到达JSON中一些深层嵌套的对象
I have a directory with roughly 500 JSON files that I need to read through, and output certain data from so I load the files like this: 我有一个目录,其中包含大约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++;
}
}
Once I have the file loaded, I am reading it through File.ReadAllText so I am doing this: 加载文件后,我将通过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;
...
I have created objects of all of the JSON using json2csharp, but when I try to access the deeply nested objects using dot notation (rootObj.ResultsReport.FinalReport.VariantProperties.VariantProperty.VariantName)
, I get an error 'Object does not contain a definition for FinalReport and no extension method FinalReport...' 我已经使用json2csharp创建了所有JSON的对象,但是当我尝试使用点表示法
(rootObj.ResultsReport.FinalReport.VariantProperties.VariantProperty.VariantName)
访问深度嵌套的对象时,出现错误消息“对象不包含定义对于FinalReport,没有扩展方法FinalReport ...”
My object definition looks like this: 我的对象定义如下:
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; }
}
The JSON looks like this: 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"
}
]
}
What am I doing wrong? 我究竟做错了什么? I've followed so many different examples but it just wont seem to work
我遵循了很多不同的例子,但它似乎无法正常工作
Write properties like 写像
public ResultsReport ResultsReport { get; set; }
public FinalReport FinalReport { get; set; }
You are using object as property type, thats wrong, it is not about JSON deserialization. 您使用对象作为属性类型,那是错误的,这与JSON反序列化无关。
As Volkan said the issue isn't the JSON deserialization. 正如Volkan所说,问题不是JSON反序列化。 I got your json working by structuring my classes like so:
我通过像这样构造类来使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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.