我编写了一些代码来读取CSV文件中的数据,并将每行中的重要值保存到我想要转换为JSON的String中。 虽然我仍然是C#的新手并且没有使用任何与JSON相关的东西,但我不知道如何以最好的方式做到这一点。
这是我到目前为止的代码:
using System;
using System.IO;
using System.Collections.Generic;
namespace CSV_Importer
{
class Program
{
static void Main(string[] args)
{
// Read the file and display it line by line.
String MedicalData;
FileStream MedicalFile = new FileStream(@"C:\CSV Health-Care cs\CsvExport_01_03_2017.csv", FileMode.Open);
StreamReader MedicalFileReader = new StreamReader(MedicalFile);
List<Participant>List = new List<Participant>();
MedicalData = MedicalFileReader.ReadLine();
while ((MedicalData = MedicalFileReader.ReadLine()) != null)
{
//MedicalData = MedicalFileReader.ReadLine();
//Console.WriteLine(MedicalData);
String[] MedicalDataArray = MedicalData.Split(';');
Participant ParticipantTemp = new Participant();
ParticipantTemp.DateOfBirth = MedicalDataArray[0];
ParticipantTemp.Gender = MedicalDataArray[1];
ParticipantTemp.BmiValue = Convert.ToDouble(MedicalDataArray[9]);
ParticipantTemp.RelativeFatMass = Convert.ToDouble(MedicalDataArray[12]);
ParticipantTemp.AbsoluteFatMass = Convert.ToDouble(MedicalDataArray[13]);
ParticipantTemp.FatFreeMassValue = Convert.ToDouble(MedicalDataArray[14]);
ParticipantTemp.SkeletalMuscleMassValue = Convert.ToDouble(MedicalDataArray[15]);
ParticipantTemp.SmmTorsoValue = Convert.ToDouble(MedicalDataArray[16]);
ParticipantTemp.SmmRlValue = Convert.ToDouble(MedicalDataArray[17]);
ParticipantTemp.SmmLlValue = Convert.ToDouble(MedicalDataArray[18]);
ParticipantTemp.SmmLaValue = Convert.ToDouble(MedicalDataArray[19]);
ParticipantTemp.SmmRaValue = Convert.ToDouble(MedicalDataArray[20]);
ParticipantTemp.WaistCircumferenceValue = Convert.ToDouble(MedicalDataArray[291]);
ParticipantTemp.WeightValue = Convert.ToDouble(MedicalDataArray[296]);
ParticipantTemp.HeightValue = Convert.ToDouble(MedicalDataArray[299]);
ParticipantTemp.TotalEnergyExpenditureValue = Convert.ToDouble(MedicalDataArray[301]);
ParticipantTemp.RestingEnergyExpenditureValue = Convert.ToDouble(MedicalDataArray[302]);
ParticipantTemp.FfmiValue = Convert.ToDouble(MedicalDataArray[307]);
ParticipantTemp.FmiValue = Convert.ToDouble(MedicalDataArray[308]);
ParticipantTemp.VisceralAdiposeTissueValue = Convert.ToDouble(MedicalDataArray[318]);
List.Add(ParticipantTemp);
//Console.WriteLine(MedicalDataArray[1]);
}
foreach (var participant in List)
{
Console.Write(participant.DateOfBirth);
Console.Write(" " + participant.Gender);
Console.Write(" " + participant.BmiValue);
Console.Write(" " + participant.RelativeFatMass);
Console.Write(" " + participant.AbsoluteFatMass);
Console.Write(" " + participant.FatFreeMassValue);
Console.Write(" " + participant.SkeletalMuscleMassValue);
Console.Write(" " + participant.SmmTorsoValue);
Console.Write(" " + participant.SmmRlValue);
Console.Write(" " + participant.SmmLlValue);
Console.Write(" " + participant.SmmLaValue);
Console.Write(" " + participant.SmmRaValue);
Console.Write(" " + participant.WaistCircumferenceValue);
Console.Write(" " + participant.WeightValue);
Console.Write(" " + participant.HeightValue);
Console.Write(" " + participant.TotalEnergyExpenditureValue);
Console.Write(" " + participant.RestingEnergyExpenditureValue);
Console.Write(" " + participant.FfmiValue);
Console.Write(" " + participant.FmiValue);
Console.WriteLine(participant.VisceralAdiposeTissueValue);
}
Console.ReadLine();
}
}
public class Participant
{
public string DateOfBirth;
public string Gender;
public double BmiValue;
public double RelativeFatMass;
public double AbsoluteFatMass;
public double FatFreeMassValue;
public double SkeletalMuscleMassValue;
public double SmmTorsoValue;
public double SmmRlValue;
public double SmmLlValue;
public double SmmLaValue;
public double SmmRaValue;
public double WaistCircumferenceValue;
public double WeightValue;
public double HeightValue;
public double TotalEnergyExpenditureValue;
public double RestingEnergyExpenditureValue;
public double FfmiValue;
public double FmiValue;
public double VisceralAdiposeTissueValue;
}
}
答案 0 :(得分:0)
Json.NET是将对象序列化为JSON的事实标准。它就像
一样简单JsonConvert.SerializeObject(MedicalDataArray);