Selected list properties to JSON in C# -
i have list 8 properties , values. of them iotype=="o" , convert json format.
if use linq
excelio.where(m => m.iotype == "o").select(m => new excelbo { interfacecolid = m.interfacecolid, iovalue = m.iovalue }).tolist(); return jsonconvert.serializeobject(excelbolist); current output
[{"functionwbid":0,"excelcellno":null,"iotype":null, "interfacecolid":"txtprmservictaxamt","iovalue":"0.00","datatype":null, "status":null,"functionwbmap":null}, {"functionwbid":0,"excelcellno":null,"iotype":null, "interfacecolid":"txtprmstampdutyamt","iovalue":"10.00", "datatype":null,"status":null,"functionwbmap":null}] desired output : "interfacecolid" value string , 'iovalue" value value.
[{"txtprmservictaxamt":"0.00","txtprmstampdutyamt" :"10.00"}] please me give desired ouput?
the simple way store values dictionary instead of list:
var excelbodict = excelio.where(m => m.iotype == "o") .todictionary(x => x.interfacecolid, y => y.iovalue); return jsonconvert.serializeobject(excelbodict); this serialized as:
[{"txtprmservictaxamt":"0.00","txtprmstampdutyamt" :"10.00"}] it work if not have repeating values of interfacecolid. if so, guess need implement own serializer based on jsontextwriter (example usage) in following way:
stringbuilder sb = new stringbuilder(); stringwriter sw = new stringwriter(sb); using (jsonwriter writer = new jsontextwriter(sw)) { writer.writestartobject(); foreach(var item in excelbolist) { writer.writepropertyname(item.interfacecolid); writer.writevalue(item.iovalue); } writer.writeend(); writer.writeendobject(); } var result = sb.tostring(); note, if have multiple properties single name - may cause unpredictable behaviour.

Comments
Post a Comment