C# get csv variables to JSON String -
i wrote code read out data csv file , save important values each line string convert json. though still new c# , didn't use json related don't know how best way.
here code have far:
using system; using system.io; using system.collections.generic; namespace csv_importer { class program { static void main(string[] args) { // read file , display line 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; } }
json.net defacto standard serializing object json. simple
jsonconvert.serializeobject(medicaldataarray);
Comments
Post a Comment