我正在解析一个如下所示的文件:
test_data = {
{
id = '001-000505',
cnv = {
e9 = 0,
i6 = 0,
},
rel = {
rs10850985 = '-',
rs38932097 = '-',
rs5030655 = '-',
rs10655852 = '-',
rs28371725 = '-',
},
result = '*5/*5',
gt = {
rs31080985 = { '-', '-' },
rs16947 = { '-', '-' },
rs3892097 = { '-', '-' },
rs503350655 = { '-', '-' },
rs50530865 = { '-', '-' },
rs5030656 = { '-', '-' },
rs106371706 = { '-', '-' },
rs59421388 = { '-', '-' },
rs7693258 = { '-', '-' },
rs28371725 = { '-', '-' },
},
},
{
id = '004-AATTGG',
cnv = {
e9 = 1,
i6 = 1,
},
rel = {
rs1080985 = '>>',
rs3892097 = '>>',
rs505306d55 = '>>',
rs1065852 = '>>',
rs2837d1725 = '>>',
},
result = '*1/*5',
gt = {
rs10830985 = { 'C', 'C' },
rs164947 = { 'C', 'C' },
rs3892097 = { 'G', 'G' },
rs5030e655 = { 'T', 'T' },
rs5030865 = { 'G', 'G' },
rs5030656 = { 'AAG', 'AAG' },
rs1065852 = { 'C', 'C' },
rs28371706 = { 'C', 'C' },
rs59421388 = { 'G', 'G' },
rs769258 = { 'G', 'G' },
rs28371725 = { 'G', 'G' },
},
},
{
id = '003-0300101',
cnv = {
e9 = 1,
i6 = 1,
},
rel = {
rs1080985 = '>>',
rs3892097 = '>>',
rs50530655 = '>>',
rs10365852 = '>>',
rs283271725 = '<<',
},
result = '*41/*5',
gt = {
rs1080985 = { 'C', 'C' },
rs16947 = { 'T', 'T' },
rs3892097 = { 'G', 'G' },
rs5030655 = { 'T', 'T' },
rs5030865 = { 'G', 'G' },
rs5030656 = { 'AAG', 'AAG' },
rs1065852 = { 'C', 'C' },
rs28371706 = { 'C', 'C' },
rs593421388 = { 'G', 'G' },
rs7659258 = { 'G', 'G' },
rs28371725 = { 'A', 'A' },
},
},
{
id = '007-CCAA',
cnv = {
e9 = 1,
i6 = 1,
},
rel = {
rs1080985 = '>>',
rs38922097 = '>>',
rs50350655 = '>>',
rs1065852 = '>>',
rs283371725 = '<<',
},
result = '*41/*5',
gt = {
rs1080985 = { 'C', 'C' },
rs16947 = { 'T', 'T' },
rs3892097 = { 'G', 'G' },
rs50350655 = { 'T', 'T' },
rs50350865 = { 'G', 'G' },
rs5030656 = { 'AAG', 'AAG' },
rs106235852 = { 'C', 'C' },
rs28371706 = { 'C', 'C' },
rs59421388 = { 'G', 'G' },
rs769258 = { 'G', 'G' },
rs28371725 = { 'A', 'A' },
},
},
{
id = '001-000105',
cnv = {
e9 = 1,
i6 = 1,
},
rel = {
rs1080985 = '>>',
rs38392097 = '>>',
rs5030655 = '>>',
rs10565852 = '>>',
rs283371725 = '>>',
},
result = '*1/*5',
gt = {
rs10820985 = { 'C', 'C' },
rs16947 = { 'C', 'C' },
rs32892097 = { 'G', 'G' },
rs53030655 = { 'T', 'T' },
rs50303865 = { 'G', 'G' },
rs50530656 = { 'AAG', 'AAG' },
rs1065852 = { 'C', 'C' },
rs283751706 = { 'C', 'C' },
rs59421388 = { 'G', 'G' },
rs769258 = { 'G', 'G' },
rs28371725 = { 'G', 'G' },
},
},
我需要解析所有这些数据并以某种数据结构捕获它。
这是我想出的:
struct LuaStructure
{
public string id { get; set; }
public struct cnv
{
public string e9 { get; set; }
public string x9 { get; set; }
public string i6 { get; set; }
}
public struct rel
{
public string rs1080985 { get; set; }
public string rs3892097 { get; set; }
public string rs5030655 { get; set; }
public string rs1065852 { get; set; }
public string rs28371725 { get; set; }
}
public string result { get; set; }
public struct gt
{
public string rs1080985 { get; set; }
public string rs16947 { get; set; }
public string rs3892097 { get; set; }
public string rs5030655 { get; set; }
public string rs5030865 { get; set; }
public string rs5030656 { get; set; }
public string rs1065852 { get; set; }
public string rs28371706 { get; set; }
public string rs59421388 { get; set; }
public string rs769258 { get; set; }
public string rs28371725 { get; set; }
}
}
我不认为这是正确的解决方案,因为我想说
string somestring=LuaStructure.gt.rs1080985;
这是不可能的,因为我需要先声明;
LuaStructure somestructure = new LuaStructure;
然后我需要做
LuaStructure.gt somestructuregt = new LuaStructure.gt;
我不想完成整个过程
我需要什么类的class / struct / enum来捕获我想要的数据?
答案 0 :(得分:1)
由于属性/类型需要显式解析和赋值,因此您无法在struct
中执行此操作。我倾向于避免这种情况,但实际上您可能会受益于使用RegEx或基本字符串操作将输入转换为JSON,因为格式非常接近。这样做,你至少可以利用现有的解析器来处理繁重的工作。