-3
CSVファイルからデータを読み込み、各行の重要な値をJSONに変換したい文字列に保存するコードを書きました。 私はまだC#をかなり新しくしていますが、JSON関連のものは何も使用していませんが、これをどのように行うのが最もよい方法はわかりません。ここでC#をJSONに変換するString
は、私が持っているコードは、これまでのところです:
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;
}
}
次のようなものですか? https://stackoverflow.com/a/10824520/4537273 – Ferryzijl
[C#オブジェクトを.NET 4のJSON文字列に変換する]の可能な複製(https://stackoverflow.com/questions/6201529/turn-c-sharp-object -into-a-json-string-in-net-4) –
この質問に関する唯一の注意点は、受け入れられた回答がMicrosoftによって推奨されておらず、代わりにJSON.Netを使用する必要があるということです。 – crashmstr